This feature is really important to my app and would really like some help on it. Basically, I want to add a UIPanGestureRecognizer to a video player so that th
Thanks for your code. I used it, changed few lines, it's working well.
@objc func handlePanGesture(sender: UIPanGestureRecognizer) {
switch sender.state {
case .began, .changed:
let translation = sender.translation(in: self.view)
let horizontalTranslation = Float(translation.x)
let durationInSeconds = Float(CMTimeGetSeconds(player.currentItem!.asset.duration))
// normalize based on duration and width of the view
let scale:Float = durationInSeconds / Float(self.view.frame.width) / 4
// then you need to add the current time of player to avoid it starting from the middle.
let seekTime = horizontalTranslation * scale + Float((self.player.currentItem?.currentTime().seconds)!)
let cmTime = CMTimeMakeWithSeconds(Float64(Float(seekTime)), preferredTimescale: self.player.currentTime().timescale)
player.seek(to: cmTime)
print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(seekTime)")
default:
print("default")
}
}