自定义UISilder实现添加点,然后可以点击,并且可以抛出事件

ARAPointSilder有两种类型

  • normal是可以滑动的
  • point是点击的也可以滑动,但是只可以停在点出
enum ARAPointSilderType: Int {
    case normal = 0
    case point
}

如何使用

自定义UISilder实现添加点,然后可以点击,并且可以抛出事件_第1张图片
Untitled 下午1.27.07.gif
        let silder = ARAPointSilder()
        silder.bounds = CGRect(x: 0, y: 0, width: 300, height: 80)
        silder.center = view.center
        silder.type = .point
        silder.continuous = false
        silder.pointWidth = 20
        silder.numberOfPoint = 4
        silder.titleAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 18)!]
        silder.titleArray = ["面议","报价","你看你","傻不傻"]
        silder.addTarget(self, action: #selector(printNumber(sender:)), for: .valueChanged)
        view.addSubview(silder)

ARAPointSilder 的链接,如果接的还好的话就给个星星吧!

ARAPointSilderDemo

ARAPointSilder具有很多的属性

// MARK: - 属性
    
    /// 滑条的类型
    var type: ARAPointSilderType = .normal
    
    /// 滑块的颜色
    var thumbColor: UIColor = UIColor.white
    
    /// 滑块的图片
    var thumbImage: UIImage?
    
    /// 滑块的大小
    var thumbSize: CGSize = CGSize(width: 25, height: 25)
    
    /// 滑块的点击范围,默认是2倍
    var thumbTouchRate: CGFloat = 2.0
    
    /// 滑块边框颜色
    var thumbBordColor: UIColor = UIColor.blue
    
    /// 类型为普通时,使用value
    var value: CGFloat = 0.0 {
        didSet {
            if value <= minimumValue {
                value = minimumValue
            }
            if value >= maximumValue {
                value = maximumValue
            }
        }
    }

    /// 最大值
    var maximumValue:CGFloat = 1.0
    
    /// 最小值
    var minimumValue:CGFloat = 0.0
    
    /// 最大值的滑动颜色
    var maximumValueTrackColor: UIColor = UIColor.cyan
    
    /// 最小值的滑动颜色
    var minimumValueTrackColor: UIColor = UIColor.red
    
    /// 值变化是否是连续的
    var continuous: Bool = true
    
    /// 类型为点,使用index
    var index: Int = 0 {
        didSet {
            if index >= numberOfPoint {
                index = numberOfPoint - 1
            } else if index < 0 {
                index = 0
            }
        }
    }
    
    /// 点数的point数 默认是5级,最低是2级
    var numberOfPoint: Int = 5 {
        didSet {
            if numberOfPoint < 2 {
                numberOfPoint = 2
            }
        }
    }
    
    /// point点击范围,默认是point大小的2倍 pointTouchRate = 2
    var pointTouchRate: CGFloat = 2.0
    
    /// 设置point的颜色
    var pointColor: UIColor = UIColor.cyan
    
    /// 设置Point的大小
    var pointWidth: CGFloat?
    
    /// 设置slider的左右间隔
    var margin: CGFloat = 30.0
    
    /// 滑条的颜色
    var lineColor: UIColor = UIColor.gray
    
    /// 设置滑条图片来显示
    var lineImage: UIImage?
    
    /// 设置滑条的粗细程度
    var lineWidth: CGFloat = 8.0
    
    /// 滑条y偏移量,默认是0
    var sliderOffset: CGFloat = 0
    
    /// 标题y偏移,默认是向下偏移20 正数向下,负数向上
    var titleOffset: CGFloat = 20
    
    /// 设置point的下标题
    var titleArray: [String]?
    
    /// 标题的字体
    var titleFont: UIFont?
    
    /// 所有标题的颜色
    var titleColor: UIColor?
    
    /// 每个标题的颜色
    var titleColorArray: [UIColor]?
    
    /// 文字的属性
    lazy var titleAttributes: [String:AnyObject] = {
        
        var dict = [String:AnyObject]()
        // 文字颜色
        dict[NSForegroundColorAttributeName] =
            self.titleColor != nil
            ? self.titleColor
            : UIColor.lightGray
        // 文字大小
        dict[NSFontAttributeName] =
            self.titleFont != nil
            ? self.titleFont
            : UIFont(name: "HelveticaNeue", size: 14)
        return dict
        
    }()
    
    private var touchPoint: CGPoint = CGPoint.zero
    
    private var thumbPoint: CGPoint = CGPoint.zero
    
    private var thumbRect: CGRect = CGRect.zero
    
    private var pointRectArray: [NSValue] = [NSValue]()
    
    private var startPoint: CGFloat = 30
    
    private var endPoint: CGFloat = 30
    
    private var y: CGFloat = 0
    
    private var isTap: Bool = false
    
    private var isRun: Bool = false

我通过重写override func draw(_ rect: CGRect) {}来实现显示

这是监听方法

override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        touchPoint = touch.location(in: self)
        var tempThumbRect = thumbRect
        tempThumbRect.size.width = tempThumbRect.size.width * thumbTouchRate
        tempThumbRect.size.height = tempThumbRect.size.height * thumbTouchRate
        tempThumbRect.origin.x -= (tempThumbRect.size.width - thumbRect.size.width) / 2.0
        tempThumbRect.origin.y -= (tempThumbRect.size.height - thumbRect.size.height) / 2.0
        
        if type == .point {
            isTap = true
            
            for objc in pointRectArray {
                let oldRect = objc.cgRectValue
                var newRect = oldRect
                newRect.size.width = newRect.size.width * pointTouchRate
                newRect.size.height = newRect.size.height * pointTouchRate
                newRect.origin.x -= (newRect.size.width - oldRect.size.width) / 2.0
                newRect.origin.y -= (newRect.size.height - oldRect.size.height) / 2.0
                if newRect.contains(touchPoint) {
                    thumbPoint = CGPoint(x: newRect.midX - thumbSize.width / 2.0,
                                         y: newRect.midY - thumbSize.height / 2.0)
                    setNeedsDisplay()
                    return true
                }
            }
        } else if type == .normal {
            return tempThumbRect.contains(touchPoint)
        }
        return false
    }

你可能感兴趣的:(自定义UISilder实现添加点,然后可以点击,并且可以抛出事件)