点击UISlider设置值

我创build了一个Slider(作为video的控件操作,就像YouTube一样),并设置最大(持续时间)和最小值。 然后使用SeekToTime更改当前时间。 现在,用户可以滑动滑块的拇指来改变该值

我想要实现的是让用户点击滑块上的任何位置并设置video的当前时间。

我从这个答案得到了一个方法,我试图将其应用到我的案件,但无法使其工作

 class ViewController: UIViewController, PlayerDelegate { var slider: UISlider! override func viewDidLoad() { super.viewDidLoad() // Setup the slider } func sliderTapped(gestureRecognizer: UIGestureRecognizer) { // print("A") let pointTapped: CGPoint = gestureRecognizer.locationInView(self.view) let positionOfSlider: CGPoint = slider.frame.origin let widthOfSlider: CGFloat = slider.frame.size.width let newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider) slider.setValue(Float(newValue), animated: true) } } 

我认为这应该工作,但没有运气。 然后我尝试着打印“A”到日志进行debugging,但是它并不显然不会进入sliderTapped()函数。

我究竟做错了什么? 还是有更好的方法来实现我想要实现的目标?

看起来像你需要实际初始化你的viewDidLoad()上面的代码示例的轻拍手势识别器。 那里有一个评论,但我没有看到任何地方创build识别器。

Swift 2:

 class ViewController: UIViewController { var slider: UISlider! override func viewDidLoad() { super.viewDidLoad() // Setup the slider // Add a gesture recognizer to the slider let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "sliderTapped:") self.slider.addGestureRecognizer(tapGestureRecognizer) } func sliderTapped(gestureRecognizer: UIGestureRecognizer) { // print("A") let pointTapped: CGPoint = gestureRecognizer.locationInView(self.view) let positionOfSlider: CGPoint = slider.frame.origin let widthOfSlider: CGFloat = slider.frame.size.width let newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider) slider.setValue(Float(newValue), animated: true) } } 

Swift 3:

 class ViewController: UIViewController { var slider: UISlider! override func viewDidLoad() { super.viewDidLoad() // Setup the slider // Add a gesture recognizer to the slider let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sliderTapped(gestureRecognizer:))) self.slider.addGestureRecognizer(tapGestureRecognizer) } func sliderTapped(gestureRecognizer: UIGestureRecognizer) { // print("A") let pointTapped: CGPoint = gestureRecognizer.location(in: self.view) let positionOfSlider: CGPoint = slider.frame.origin let widthOfSlider: CGFloat = slider.frame.size.width let newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider) slider.setValue(Float(newValue), animated: true) } } 

它似乎只是inheritanceUISlider,并始终返回到beginTracking产生所需的效果。

iOS 10和Swift 3

 class CustomSlider: UISlider { override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { return true } } 

之后在代码中使用CustomSlider而不是UISlider。

这是我的代码,基于“myuiviews”的答案。
我修复了原始代码的两个小错误。

1 – 0攻击太困难了,所以我简化了
2 – 滑动滑块的拇指只是一点点,也发射“tapGestureRecognizer”,这使得它回到了初始位置,所以我加了一个最小距离filter,以避免这一点。

斯威夫特4

 class ViewController: UIViewController { var slider: UISlider! override func viewDidLoad() { super.viewDidLoad() // Setup the slider // Add a gesture recognizer to the slider let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sliderTapped(gestureRecognizer:))) self.slider.addGestureRecognizer(tapGestureRecognizer) } @objc func sliderTapped(gestureRecognizer: UIGestureRecognizer) { // print("A") var pointTapped: CGPoint = gestureRecognizer.location(in: self.view) pointTapped.x -= 30 //Subtract left constraint (distance of the slider's origin from "self.view" let positionOfSlider: CGPoint = slider.frame.origin let widthOfSlider: CGFloat = slider.frame.size.width //If tap is too near from the slider thumb, cancel let thumbPosition = CGFloat((slider.value / slider.maximumValue)) * widthOfSlider let dif = abs(pointTapped.x - thumbPosition) let minDistance: CGFloat = 51.0 //You can calibrate this value, but I think this is the maximum distance that tap is recognized if dif < minDistance { print("tap too near") return } var newValue: CGFloat if pointTapped.x < 10 { newValue = 0 //Easier to set slider to 0 } else { newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider) } slider.setValue(Float(newValue), animated: true) } } 

pteofil的答案应该是这里接受的答案。

以下是Xamarin为大家感兴趣的解决scheme:

 public override bool BeginTracking(UITouch uitouch, UIEvent uievent) { return true; } 

正如pteofil提到的,你需要inheritanceUISlider的工作。