如何在 UIImageView 上设置 UIGestureRecognizer?

发布于 2024-12-19 17:33:12 字数 90 浏览 2 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长梦不多时 2024-12-26 17:33:12

创建 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];

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

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];
莫言歌 2024-12-26 17:33:12

是否设置

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

在附加 UIGestureRecognizer 之前

为 true?如果不尝试,

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

Have you set

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

to true before you attach the 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文