iOS 自定義 UISlider 的 trackRect

ATMMM發表於2018-08-23

iOS 自定義 UISlider 的 trackRect

自定義 slider 需要定義 UISlider 的子類,然後重寫下面兩個方法

public class LQTimeSlider: UISlider {
    override open func trackRect(forBounds bounds: CGRect) -> CGRect {
        let trackHeigt:CGFloat = 2
        let position = CGPoint(x: 0 , y: 15)
        let customBounds = CGRect(origin: position, size: CGSize(width: bounds.size.width, height: trackHeigt))
        super.trackRect(forBounds: customBounds)
        return customBounds
    }

    override open func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        
        var newRect = rect
        newRect.origin.x -= 10
        newRect.origin.y += 1
        newRect.size.width += 20
        return super.thumbRect(forBounds: bounds, trackRect: newRect, value: value).insetBy(dx: 10, dy: 10)
    }
}
複製程式碼

相關文章