How to add "UISwipeGestureRecognizer" and "TapGestureRecognizer" in Same viewcontroller in swift

61 Views Asked by At

I need left swipe gesture for my viewcontroller and in the same viewcontroller i have one imageView if i click that imageView i need to use "UITapGestureRecognizer" and push to another viewcontroller to show that full image in another viewcontroller

code: here respondToSwipeGesture is working i am able to go back but tap gesture for profileImageView is not working. when i tap on image break point also not hitting. how to make it work. please guide me.

class CommentViewController: UIViewController, UIGestureRecognizerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.interactivePopGestureRecognizer?.delegate = self        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizer.Direction.left
    self.view.addGestureRecognizer(swipeRight)
    
    profileImageView.addTapGestureRecognizer { [weak self] in
        self?.gotoStudentProfile()
    }
}

private func gotoStudentProfile() {
let vc = Helper.getVcObject(vcName: .StudentProfileViewController, storyboardName: .Profile) as! StudentProfileViewController
checkAndPushPop(vc, navigationController: navigationController)
 }

fileprivate typealias Action = (() -> Void)?

fileprivate var tapGestureRecognizerAction: Action? {
    set {
        if let newValue = newValue {
            // Computed properties get stored as associated objects
            objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
    get {
        let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
        return tapGestureRecognizerActionInstance
    }
}

 public func addTapGestureRecognizer(action: (() -> Void)?) {
    self.isUserInteractionEnabled = true
    self.tapGestureRecognizerAction = action
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
    self.addGestureRecognizer(tapGestureRecognizer)
 }  

@objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
    if let action = self.tapGestureRecognizerAction {
        action?()
    } else {
        print("no action")
    }
}
2

There are 2 best solutions below

0
Maksym Musiienko On BEST ANSWER

By default, UIImageView has isUserInteractionEnabled set to false. You have to set it to true. Also, since you have not shared the implementation of the addTapGestureRecognizer method, make sure that it does not have any bugs.

0
Om Sanatan On
profileImageView.addTapGestureRecognizer

above this code set isUserInteractionEnabled enable for image

profileImageView.isUserInteractionEnabled = true

Updated

    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))

    profileImageView.addGestureRecognizer(tap)

    profileImageView.isUserInteractionEnabled = true
    
    // function which is triggered when handleTap is called
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("Hello World")
    }

Try this way and let me know