UITapGestureRecognizer not working in UIImageView

好久不见. 提交于 2019-12-18 18:38:06

问题


I had the following code:

UITapGestureRecognizer *showStoryTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[showStoryTapRecognizer setDelegate:self];
[self.storyImageView_ addGestureRecognizer:showStoryTapRecognizer];
[showStoryTapRecognizer release];

This however doesn't trigger the showNewsStory, why is this? I have enabled userInteraction in the image view.


回答1:


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

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;



回答2:


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.




回答3:


UIImageView is the only subclass of UIView inside of UIKit that has user interaction disabled by default.




回答4:


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
}()



回答5:


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;



回答6:


objective-c ios 10

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

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;

// swift 3.0

   let oneTouch = UITapGestureRecognizer(target: self, action: #selector(self.OneTouchHandeler(_:)))

imageView.addGestureRecognizer(oneTouch)

imageView.isUserInteractionEnabled = true



回答7:


If you have already set imageView.userInteractionEnabled = YES; but the action still doesn't fire. Maybe it's because for one of superviews of imageView userInteractionEnabled is NO;




回答8:


if you allowed 2 different gesture, you should add below code snippet. For example, you use pickerView and also you want to detect tap gesture for same pickerView.

Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.

Objective C

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return true;
}

Swift

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}


来源:https://stackoverflow.com/questions/11552184/uitapgesturerecognizer-not-working-in-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!