如何允许在滚动视图上触摸图像?

发布于 2024-12-25 00:47:02 字数 212 浏览 2 评论 0原文

在我的程序中,我使用滚动视图作为背景视图。然后,我在滚动视图上添加几个图像。另外,我允许用户触摸图像以触发一些操作。但是,触摸滚动视图上附加的图像时无法检测到它们。我在触摸事件中使用以下代码:

UITouch * touch = [[event allTouches] anyObject];

我做错了什么吗?我该如何解决这个问题?非常感谢!

In my program, I use a scroll view as the background view. Then, I add several images on the scroll view. Also, I allow the users to touch the image in order to trigger some actions. However, the images attached on the scroll view cannot be detected when they were touched. I use the following code my the touch event:

UITouch * touch = [[event allTouches] anyObject];

Did I do something wrong? How can I solve this problem? Thank you very much!

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

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

发布评论

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

评论(1

橙幽之幻 2025-01-01 00:47:02

您可以使用 UIImageView 来跟踪触摸,并设置 myImageView.userInteractionEnabled = YES,因为 imageview 的默认设置为 no。

如果你想检测图像上的点击,你可以这样做:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesSingleTap:)];
        [singleTap setNumberOfTapsRequired:1];

        [imageView addGestureRecognizer:singleTap];

并实现你的选择器:

- (void)handlesSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    //Handle touch
}

You can use UIImageView to track touches, and also set myImageView.userInteractionEnabled = YES, since the default is no for imageviews.

If you want to detect a tap on the image you can do this:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesSingleTap:)];
        [singleTap setNumberOfTapsRequired:1];

        [imageView addGestureRecognizer:singleTap];

and implement your selector:

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