How to get the center of the thumb image of UISlider

后端 未结 12 1846
挽巷
挽巷 2020-11-29 23:18

I\'m creating a custom UISlider to test out some interface ideas. Mostly based around making the thumb image larger.

I found out how to do that, <

12条回答
  •  臣服心动
    2020-11-29 23:37

    This will work for the UISlider being placed anywhere on the screen. Most of the other solutions will only work when the UISlider is aligned with the left edge of the screen. Note, I used frame rather than bounds for the thumbRect, to achieve that. And I show two variations, based on using frame or bounds for the trackRect

    extension UISlider {
    
        //this version will return the x coordinate in relation to the UISlider frame
        var thumbCenterX: CGFloat {
            return thumbRect(forBounds: frame, trackRect: trackRect(forBounds: bounds), value: value).midX
        }
    
        //this version will return the x coordinate in relation to the UISlider's containing view
        var thumbCenterX: CGFloat {
            return thumbRect(forBounds: frame, trackRect: trackRect(forBounds: frame), value: value).midX
        }
    
    }
    

提交回复
热议问题