通过滑动设置 UIImageView

发布于 2024-09-24 15:33:02 字数 139 浏览 8 评论 0原文

我目前使用左右箭头在图像之间切换,但想添加功能,以便用户可以沿方向滑动来更改图像。

设备如何检测向左滑动或向右滑动并将其用作 IBAction 或类似于触发执行的按钮?

是否有内置方法或者是否需要从头开始编码?

谢谢

I currently use a left and right arrow to switch between images, but would like to add functionality so that the user can swipe in the direction to change the image.

How does the device detect a left swipe or right swipe and use that as an IBAction or similar to the button triggering execution?

Is there a build in method or does it need to be coded from scratch?

Thanks

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

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

发布评论

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

评论(2

街角卖回忆 2024-10-01 15:33:02

如果您的目标是 3.2 或更高,您可以使用 UISwipeGestureRecognizer。将其放入您的 UIImageView 子类中:

- (void)viewDidLoad {
    [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    [recognizer setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:recognizer];
    [recognizer release];
}

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
     //do something
}

如果您的目标是 3.2 或更低版本,则需要使用 NSClassFromString & [[UIDevice currentDevice] systemVersion] 确保当前设备具有所需的类。

If your target is 3.2 or higher, you can use UISwipeGestureRecognizer. Put this in your UIImageView subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

   UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    [recognizer setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:recognizer];
    [recognizer release];
}

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
     //do something
}

If you're targeting 3.2 or lower, you'll need to make use of NSClassFromString & [[UIDevice currentDevice] systemVersion] to make sure the current device has the required class.

深空失忆 2024-10-01 15:33:02

除了 christo 所说的之外,我仍然建议以“3.2 之前”的方式执行此操作,因为您将无法支持尚未更新到 4.0 的 iPhone,因此每个第一代 iPhone 都存在。

旧的方法是覆盖 UIImageView 子类中的touchesBegan/Moved/Ended。 这篇文章是关于 TableViews 的,但完全相同的代码适用于 UIView 的每个子类,因此您可以在 ImageView 中使用它。

In addition to what christo said, I would still recommend to do this the "before 3.2"-Way since you won't be able to support iPhones, which have not yet updated to 4.0, so consequently every 1. gen iPhone out there.

The old way is to overwrite touchesBegan/Moved/Ended in your UIImageView subclass. This post is about TableViews but the exact same code works for every subclass of UIView, so you can use it in your ImageView.

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