xcode 如何在 UIImageView 上设置 UIGestureRecognizer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8369908/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:39:30  来源:igfitidea点击:

How can I set up a UIGestureRecognizer on a UIImageView?

iphoneobjective-ciosxcodeuigesturerecognizer

提问by pixelbitlabs

How can I add a UIGestureRecognizer to recognise left or right swipes on top of a UIImageView? I just need an action to be called when the swipe is left and another action for when the swipe is right.

如何添加 UIGestureRecognizer 以识别 UIImageView 顶部的向左或向右滑动?我只需要在向左滑动时调用一个动作,在向右滑动时调用另一个动作。

回答by Louie

Create 2 swipe gestures and add them to your image view.

创建 2 个滑动手势并将它们添加到您的图像视图中。

UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myRightAction)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [myImageView addGestureRecognizer:recognizer];
    [recognizer release];

UISwipeGestureRecognizer * recognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myLeftAction)];
    [recognizer2 setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [myImageView addGestureRecognizer:recognizer2];
    [recognizer2 release];

回答by DanZimm

Have you set

你设置了吗

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

to true before you attach the UIGestureRecognizer?

在附加之前为真UIGestureRecognizer

If not try that

如果不试试

If that doesn't work, you could add a temp view and add the gesture recognizer to the temp view and then add the imageview to the temp view

如果这不起作用,您可以添加临时视图并将手势识别器添加到临时视图,然后将图像视图添加到临时视图