Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer not working in UIImageView

People also ask

How do I use UITapGestureRecognizer?

The iOS UITapGestureRecognizer class has a built-in way to detect a double tap on any view. All you need to do is create the recognizer, set its numberOfTapsRequired property to 2, then add it to the view you want to monitor.

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


You should enable user interaction for UIImageView object:

[self.storyImageView_ setUserInteractionEnabled:YES];

EDIT:

Try to remove the

[showStoryTapRecognizer setDelegate:self];

I don't think UITapGestureRecognizer has its delegate methods as far as I know.


UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;

UIImageView has user interaction disabled by default, unlike most other UIView subclasses in UIKit.


I also noticed that in swift3, if you are adding a gesture recognizer which also looks for the target and the target is usually self, then you have to make the UIView to which you are adding the gesture recognizer to be a lazy var. Otherwise the gesture recognizer won't work. I think this is a bug in swift3. Ideally if you are accessing self in a variable before the class is fully initialized, it should throw an error. The code below won't detect gesture recognizer.

let messageImageView: CachedImageView = {
    let iv = CachedImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.layer.cornerRadius = 16
    iv.layer.masksToBounds = true
    iv.contentMode = .scaleAspectFill
    iv.isUserInteractionEnabled = true
    let zoomTap = UITapGestureRecognizer(target: self, action: #selector(handleZoomTap))
    zoomTap.numberOfTapsRequired = 1
    iv.addGestureRecognizer(zoomTap)
    return iv
}()

To fix that, you have to use lazy var

lazy var messageImageView: CachedImageView = {
    let iv = CachedImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.layer.cornerRadius = 16
    iv.layer.masksToBounds = true
    iv.contentMode = .scaleAspectFill
    iv.isUserInteractionEnabled = true
    let zoomTap = UITapGestureRecognizer(target: self, action: #selector(handleZoomTap))
    zoomTap.numberOfTapsRequired = 1
    iv.addGestureRecognizer(zoomTap)
    return iv
}()

Maybe ... action:@selector(showNewsStory) instead of action:@selector(showNewsStory:) . Please check it . Are there any other UITapGestureRecognizer in this controller ? Try this:

otherTapRecognizer.cancelsTouchesInView = NO;