使用 Swift 增加 UISlider 的触摸目标大小
Increase touch target size of UISlider with Swift
我设法用 Swift
增加了我的 UISlider
的拇指大小,但触摸区域对于我的应用程序来说仍然太小。
如何以编程方式增加 UISlider
的触摸区大小?
我应该自己重新实现一个自定义滑块吗?
谢谢!
Subclass UISlider
并在滑块的自定义 class 中添加此方法,
func pointInside(_ point: CGPoint, withEvent event: UIEvent?) -> Bool {
var bounds: CGRect = self.bounds
bounds = CGRectInset(bounds, -10, -15)
return CGRectContainsPoint(bounds, point)
}
然后在使用滑块时创建该子class的对象。
如果您在 interfacebuilder
(情节提要)中使用了滑块,则将其 class 设置为 identity inspector
中的自定义滑块 class。
Swift 3 Lion 的精彩回答更新:
import UIKit
class CustomSlider: UISlider {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
var bounds: CGRect = self.bounds
bounds = bounds.insetBy(dx: -10, dy: -15)
return bounds.contains(point)
}
}
我设法用 Swift
增加了我的 UISlider
的拇指大小,但触摸区域对于我的应用程序来说仍然太小。
如何以编程方式增加 UISlider
的触摸区大小?
我应该自己重新实现一个自定义滑块吗?
谢谢!
Subclass UISlider
并在滑块的自定义 class 中添加此方法,
func pointInside(_ point: CGPoint, withEvent event: UIEvent?) -> Bool {
var bounds: CGRect = self.bounds
bounds = CGRectInset(bounds, -10, -15)
return CGRectContainsPoint(bounds, point)
}
然后在使用滑块时创建该子class的对象。
如果您在 interfacebuilder
(情节提要)中使用了滑块,则将其 class 设置为 identity inspector
中的自定义滑块 class。
Swift 3 Lion 的精彩回答更新:
import UIKit
class CustomSlider: UISlider {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
var bounds: CGRect = self.bounds
bounds = bounds.insetBy(dx: -10, dy: -15)
return bounds.contains(point)
}
}