I created a Slider (operating as control of the video, like YouTube has at the bottom) and set the maximum (duration) and minimum values. And then used SeekToTime
to change the currentTime. Now, the user can slide the thumb of the slider to change the value
What I want to achieve is letting the user tap on anywhere on the slider and set the current time of the video.
I got an approach from this answer, and I tried to apply it to my case, but couldn't make it work
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)
}
}
I thought this should work, but no luck. Then I tried debugging with trying to printing "A" to logs, but it doesn't so apparently it doesn't go into the sliderTapped()
function.
What am I doing wrong? Or is there a better way to achieve what I am trying to achieve?
This is my code, based on "myuiviews" answer.
I fixed 2 little "bugs" of the original code.
1 - Tapping on 0 was too difficult, so I made it easier
2 - Sliding the slider's thumb just a little bit was also firing the "tapGestureRecognizer", which makes it return to the initial position, so I added a minimum distance filter to avoid that.
Swift 4
The probably simplest solution would be, using the "touch up inside" action, connected trough the interface builder.
This will get called as soon as your finger leaves the phone screen.
It seems like just subclassing UISlider and returning always true to the beginTracking produce the desired effect.
iOS 10 and Swift 3
Use the CustomSlider instead of UISlider afterwards in your code.
Looks like you need to actually initialize the tap gesture recognizer in your viewDidLoad() per the code example above. There's a comment there, but I don't see the recognizer being created anywhere.
Swift 2:
Swift 3:
pteofil's answer should be the accepted answer here.
Below is the solution for Xamarin for everyone's interested:
As pteofil mentioned, you need to subclass the UISlider for this to work.