ios UITapGestureRecognizer 在 UIImageView 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11552184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITapGestureRecognizer not working in UIImageView
提问by xonegirlz
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.
然而,这不会触发 showNewsStory,这是为什么呢?我在图像视图中启用了 userInteraction。
回答by Kjuly
You should enable user interaction for UIImageViewobject:
您应该为UIImageView对象启用用户交互:
[self.storyImageView_ setUserInteractionEnabled:YES];
EDIT:
Try to remove the
[showStoryTapRecognizer setDelegate:self];
编辑:
尝试删除
[showStoryTapRecognizer setDelegate:self];
I don't think UITapGestureRecognizerhas its delegate methods as far as I know.
UITapGestureRecognizer据我所知,我认为没有它的委托方法。
回答by Kjuly
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];
[oneTouch setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:oneTouch];
imageView.userInteractionEnabled = YES;
回答by Radif Sharafullin
UIImageViewis the only subclass of UIViewinside of UIKitthat has user interaction disabled by default.
UIImageView是UIViewinside的唯一子类,UIKit默认情况下禁用用户交互。
回答by quantdaddy
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.
我还注意到,在 swift3 中,如果您添加的手势识别器也查找目标并且目标通常是self,那么您必须将要添加手势识别器的 UIView 设为lazy var. 否则手势识别器将无法工作。我认为这是swift3中的一个错误。理想情况下,如果您在类完全初始化之前访问变量中的 self ,它应该抛出错误。下面的代码不会检测手势识别器。
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
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
}()
回答by Carina
Maybe ... action:@selector(showNewsStory)instead of action:@selector(showNewsStory:).
Please check it .
Are there any other UITapGestureRecognizerin this controller ?
Try this:
也许......action:@selector(showNewsStory)而不是 action:@selector(showNewsStory:)。请检查一下 。UITapGestureRecognizer这个控制器中还有其他的吗?尝试这个:
otherTapRecognizer.cancelsTouchesInView = NO;
回答by dahiya_boy
objective-c ios 10
目标-c ios 10
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];
[oneTouch setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:oneTouch];
imageView.userInteractionEnabled = YES;
// swift 3.0
// 快速 3.0
let oneTouch = UITapGestureRecognizer(target: self, action: #selector(self.OneTouchHandeler(_:)))
imageView.addGestureRecognizer(oneTouch)
imageView.isUserInteractionEnabled = true
回答by dopcn
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;
如果您已经设置imageView.userInteractionEnabled = YES;但动作仍然没有触发。也许是因为 imageView 的超级视图之一 userInteractionEnabled 是 NO;
回答by Emre Gürses
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.
如果您允许 2 种不同的手势,则应添加以下代码片段。例如,您使用pickerView 并且您还想检测同一pickerView 的点击手势。
Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.
询问代理是否应该允许两个手势识别器同时识别手势。
Objective C
目标 C
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return true;
}
Swift
迅速
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
回答by Amit Thakur
use lazy var instead of let . Here is an example.
lazy var loginSignUpView: LoginSignUpView = {
let lsv = LoginSignUpView()
lsv.loginRegisterButton.addTarget(self, action: #selector(handleLoginRegistrationButton), for: .touchUpInside)
lsv.segmentedControlForLoginRegister.addTarget(self, action: #selector(handleSegmentedControlForLoginRegister), for: .valueChanged)
lsv.profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
return lsv
}()
@objc func handleSelectProfileImageView() {
print("Image tapped")
}

