title: Swift: Changing (translate) a UIView position through a pan gesture in its superview window
body: <h1>Introduction</h1>
  
  <p><strong>Context:</strong></p>
  
  <p>In my main ViewController I have a scrollView with a few objects inside (which are UIViews). When one of the UIViews are tapped/selected I animate forward a UITextView in a UIView to go with the selected object. (only one UIView can appear at a time)</p>
  
  <p>This UIView that appears on object selection is separated into a separate class called <code>AdjunctiveTextView</code>. </p>
  
  <p><strong>Issue/goal:</strong></p>
  
  <p>(the example code provided below will clear make this clear, I've also commented where the issue lies in the code)</p>
  
  <p>When an object has been tapped and has an adjacent UIView with a text I want to have that adjacent UIView to follow with the scrollView. </p>
  
  <ul>
  <li>I'm using a UIPanGestureRecognizer to attempt to do this. But I can't figure out how to make it work when the user drags in the scrollview. It only work if the user drags on the actual adjunctiveTextView. </li>
  <li>Everything works as expected except that the adjunctiveTextView does not change its position during the panGesture.</li>
  <li>I would like (if possible) to have the AdjunctiveTextView as a separate class. My ViewController file is getting rather big.</li>
  </ul>
  
  <p><strong>Question:</strong></p>
  
  <p>Why doesn't the UIPanGestureRecognizer work as expected? What is needed in order for it to translate the backView correctly?</p>
  
  <h1>Code</h1>
  
  <p><strong>My attempt: (as shown below)</strong></p>
  
  <p>My attempt simply makes the backView itself "dragable" around through the panGesture. Nothing happens to it when I scroll the scrollView.</p>
  
  <p>(I have only included relevant portions of my code)</p>
  
  <pre><code>class ViewController: UIViewController {
      let adjunctiveTextView = AdjunctiveTextView()
  
      // this is a delegate method which gets called when an object is tapped in the scrollView
      func scrollViewObjectIsTapped(_ objectScrollView: ObjectScrollView, object: AvailableObject) {
  
     **   adjunctiveTextView.scrollView = scrollView // **Edited! (scrollView is the name of the scrollView in this class too)
          adjunctiveTextView.showView(passInObject: AvailableObject)
      }
  
  }
  
  class AdjunctiveTextView: NSObject {
       lazy var backView: UIView = {
           //backView setup
       }
       lazy var textView: UITextView = {
           //textView setup
       }
  
        //additional init and setup
   **  weak var scrollView : UIScrollView! // **Edited!
       func showView(passInObject: AvailableObject) {
           if let window = UIApplication.shared.keyWindow {
  
               // the issue must either be here in the PanGesture setup
               let panG = UIPanGestureRecognizer(target: self, action: #selector(translateView(sender:)))
               panG.cancelsTouchesInView = false
              // window.addGestureRecognizer(panG) 
         **    scrollView.addGestureRecognizer(panG) // **Edited!
               window.addSubview(backView)
  
               textView.text = passInObject.information
               backView.frame = CGRect(x: passInObject.frame.minX, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)
               backView.alpha = 0
  
               //it animates a change of the backViews x position and alpha.
               UIView.animate(withDuration: 0.42, delay: 0, options: .curveEaseInOut, animations: {
                   self.backView.alpha = 1
                   self.backView.frame = CGRect(x: passInObject.frame.minX + passInObject.frame.width, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)
  
               }, completion: nil)
           }
       }
  
       // or the issue is here in the handle function for the PanGesture.
       @objc private func translateView(sender: UIPanGestureRecognizer) {
  
            if let window = UIApplication.shared.keyWindow {
               let translation = sender.translation(in: window)  //Have tried setting this to scrollView also
               switch sender.state {
               case .began, .changed:
                   backView.center = CGPoint(x: backView.center.x, y: backView.center.y + translation.y)
                   sender.setTranslation(CGPoint.zero, in: window) //Have tried setting this to sccrollView also
                   break
               case .ended:
                   break
               default:
                   break
               }
  
           }
       }
  
  
  }
  </code></pre>
  
  <p>Thanks for reading my question.</p>