0

I'm trying to call an action when I tap on a UIImage at the time of its animation. I've seen similar questions, but I could not apply these solutions to my case. Please help.

xcode 9.2 swift 4

import UIKit

class MyCalssViewController: UIViewController {

    @IBOutlet weak var myImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // action by tap
        let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action:  #selector (self.actionUITapGestureRecognizer))
        myImageView.addGestureRecognizer(gestureSwift2AndHigher)

    }

    // action by tap
    @objc func actionUITapGestureRecognizer (){

        print("actionUITapGestureRecognizer - works!") // !!! THIS DOES NOT WORK !!!

    }

    // hide UIImage before appear
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        myImageView.center.y += view.bounds.height

    }

    // show UIImage after appear with animation
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIView.animate(withDuration: 10, animations: {
            self.myImageView.center.y -= self.view.bounds.height
        })
    }
}
iOS Dev
  • 53
  • 1
  • 9

3 Answers3

1

You need to pass in the option .allowUserInteraction like this:

UIView.animate(withDuration: 10, delay: 0, options: .allowUserInteraction, animations: {
    ...
})
samwize
  • 25,675
  • 15
  • 141
  • 186
0

You just missed a line, enabling user interaction on the view.

override func viewDidLoad() {
    super.viewDidLoad()

    // action by tap
    let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action:  #selector (self.actionUITapGestureRecognizer))
    myImageView.isUserInteractionEnabled = true
    myImageView.addGestureRecognizer(gestureSwift2AndHigher)

}
trndjc
  • 11,654
  • 3
  • 38
  • 51
0
  let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.clickedImageView(_:)))
  myImageview.isUserInteractionEnabled = true
  myImageview.addGestureRecognizer(tapGesture)

// MARK: - Gesture Recognizer action

    func clickedImageView(_ sender: UITapGestureRecognizer) {
       // Write your action here.
    }
Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
  • this part " _ sender: UITapGestureRecognizer " nothing to changed – iOS Dev Dec 28 '17 at 07:28
  • sender will pass the particular UITapGestureRecognizer for validation purpose – Kathiresan Murugan Dec 28 '17 at 07:29
  • i understand, but action by tap still don't work when ImageView animate – iOS Dev Dec 28 '17 at 07:32
  • Can you share animation function – Kathiresan Murugan Dec 28 '17 at 07:33
  • Please, look higher, i already do it in my question " // hide UIImage before appear override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) myImageView.center.y += view.bounds.height } // show UIImage after appear with animation override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) UIView.animate(withDuration: 10, animations: { self.myImageView.center.y -= self.view.bounds.height }) } " – iOS Dev Dec 28 '17 at 07:37
  • I think your View was uppending above imageview. for testing purpose hide the view and check once. – Kathiresan Murugan Dec 28 '17 at 07:41
  • sorry, but I'm not sure, that understand you – iOS Dev Dec 28 '17 at 07:45
  • Ya bro, I understood. I have a doubt while animation that view is coming above view. Check the Stack of the Imageview & View position – Kathiresan Murugan Dec 28 '17 at 07:47