title: Pan Gesture with AVPlayer
body: <p>This feature is really important to my app and would really like some help on it. Basically, I want to add a <code>UIPanGestureRecognizer</code> to a video player so that the user can fast forward/rewind the video with the gesture. </p>
  
  <p>Apple has some sample code which uses Swift 3 and has the entire video player already created. The only thing missing is the <code>UIPanGestureRecognizer</code>. This is the link: <a href="https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103" rel="nofollow noreferrer">https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103</a></p>
  
  <p>In <code>viewWillAppear</code> I added the gesture like so:</p>
  
  <pre><code>let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
  view.addGestureRecognizer(panGesture)
  </code></pre>
  
  <p>This is the code that is working somewhat. It is currently scrubbing. </p>
  
  <p><strong>The problem is that every time I start the pan gesture, the video    skips to the middle and I start from there. Instead of fast    forwarding/rewinding from where the video currently is, the pan    gesture skips the video to the middle and then allows me to fast    forward/rewind which is not good.</strong></p>
  
  <pre><code>func handlePanGesture(sender: UIPanGestureRecognizer) {
      switch sender.state {
      case .began, .changed:
          let translation = sender.translation(in: self.view)
          var horizontalTranslation = Float(translation.x)
  
          let durationInSeconds = Float(CMTimeGetSeconds(player.currentItem!.asset.duration))
  
          // Using 275 as the limit for delta along x
          let translationLimit: Float = 275
          let minTranslation: Float = -1 * translationLimit
          let maxTranslation: Float = translationLimit
  
          if horizontalTranslation &gt; maxTranslation {
              horizontalTranslation = maxTranslation
          }
  
          if horizontalTranslation &lt; minTranslation {
              horizontalTranslation = minTranslation
          }
  
          let timeToSeekTo = normalize(delta: horizontalTranslation , minDelta: minTranslation, maxDelta: maxTranslation, minDuration: 0, maxDuration: durationInSeconds)
          print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(timeToSeekTo)")
  
          let cmTime = CMTimeMakeWithSeconds(Float64(timeToSeekTo), self.player.currentTime().timescale)
          player.seek(to: cmTime)
  
      default:
          print("default")
      }
  
  }
  
  func normalize(delta: Float, minDelta: Float, maxDelta: Float, minDuration: Float, maxDuration: Float) -&gt; Float {
  
      let result = ((delta - minDelta) * (maxDuration - minDuration) / (maxDelta - minDelta) + minDuration)
      return result
  }
  </code></pre>
  
  <p>Any help would be great - thanks!</p>