title: Swift, Xcode: Using PanGestureRecognizer for quiz app's answer options
body: <p>Currently I'm building a quiz app with the PanGestureRecognizer functional. The idea is that user can drag one of the answer option to 'questionMark' cell and drop it there. The app should be able to check whether a dropped answer option in the 'questionMark' cell is correct or not. </p>
  
  <p>So here is <strong>the question</strong>: how can I write a user answerCheck function for this app? </p>
  
  <p>Please see below the code and related screens from my app. 
  To do that per understanding the following steps should be executed: </p>
  
  <p>1) check whether sender.currentImage <strong>intersected</strong> 'questionMark' cell (which is UIImageView by nature) or not (if yes, it means that an answer was submitted by user by adding answer option in the 'questionMark' cell). </p>
  
  <p>2) '<strong>correct answr</strong>' is the answer which equals to the prescribed 'correct' answer option, however it should be located in 'questionMark' cell. Consequently, app should check whether sender.currentImage equals to the <strong>correct answer</strong> or not. Of course further actions depends on user answer (correct or wrong).`import UIKit</p>
  
  <p>Obviously my checking function does not work at all. There are even error from Xcode (because of different types 'Bool' and UIImage). So need help, please let me know if my post is not clear and additional comments are required. </p>
  
  <p>class MainViewController: UIViewController {</p>
  
  <pre><code>@IBOutlet weak var questionImageView: UIImageView!
  @IBOutlet weak var questionMarkView: UIImageView!
  
  @IBOutlet weak var buttonLabel1: UIButton!
  @IBOutlet weak var buttonLabel2: UIButton!
  @IBOutlet weak var buttonLabel3: UIButton!
  
  //This var was added to access 'TestBrain' struct and its functions
  var testBrain = TestBrain()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  </code></pre>
  
  <p>//GestureRecognizer->>>
        buttonLabel1.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture1)))
        buttonLabel2.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture2)))
        buttonLabel3.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture3)))
      }</p>
  
  <pre><code>@IBAction func answerButtonPressed(_ sender: UIButton) {
  
      let userAnswer = sender.currentImage!
      let correctAnswer = buttonLabel3.imageView!.frame.intersects(questionMarkView.frame)
  
  
      if userAnswer == correctAnswer {
                print("Correct")enter image description here
            } else {
                print("Wrong")
                }
  
  
  }
  
  
  // ObjC functions for GestureRecognizer #1 -&gt;&gt;&gt;
  @objc func handlePanGesture1(gesture1: UIPanGestureRecognizer) {
          let answerOptionView1 = gesture1.view!
  
          switch gesture1.state {
              case .began, .changed:
                  let translation1 = gesture1.translation(in: self.view)
                  buttonLabel1.transform = CGAffineTransform(translationX: translation1.x, y: translation1.y)
              case .ended:
                  if answerOptionView1.frame.intersects(questionMarkView.frame) {
                      answerOptionView1.frame = questionMarkView.frame
                      answerOptionView1.isUserInteractionEnabled = false
                  } else {
                      UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
                           self.buttonLabel1.transform = .identity
                       })
                  }
              default:
                  break
          }
     }
  
  // ObjC functions for GestureRecognizer #2 -&gt;&gt;&gt;
  @objc func handlePanGesture2(gesture2: UIPanGestureRecognizer) {
          let answerOptionView2 = gesture2.view!
  
          switch gesture2.state {
          case .began, .changed:
              let translation2 = gesture2.translation(in: self.view)
                    buttonLabel2.transform = CGAffineTransform(translationX: translation2.x, y: translation2.y)
          case .ended:
              if answerOptionView2.frame.intersects(questionMarkView.frame) {
                        answerOptionView2.frame = questionMarkView.frame
                        answerOptionView2.isUserInteractionEnabled = false
              } else {
                  UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
                             self.buttonLabel2.transform = .identity
                         })
                    }
          default: break
            }
       }
  
  
  @objc func handlePanGesture3(gesture3: UIPanGestureRecognizer) {
      let answerOptionView3 = gesture3.view!
  
      switch gesture3.state {
      case .began, .changed:
          let translation3 = gesture3.translation(in: self.view)
          buttonLabel3.transform = CGAffineTransform(translationX: translation3.x, y: translation3.y)
      case .ended:
          if answerOptionView3.frame.intersects(questionMarkView.frame) {
              answerOptionView3.frame = questionMarkView.frame
              answerOptionView3.isUserInteractionEnabled = false
          } else {
              UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
                  self.buttonLabel3.transform = .identity
              })
          }
      default: break
      }
  }
  </code></pre>
  
  <p>}`</p>
  
  <p>[1]: <a href="https://i.stack.imgur.com/UAFpS.jpg" rel="nofollow noreferrer">https://i.stack.imgur.com/UAFpS.jpg</a> - this is screen of my project, if necessary I can provide you with the whole project itself. </p>