如何检测 UIScrollview 中 UIImageView 的触摸?

发布于 2024-10-17 19:19:36 字数 310 浏览 5 评论 0原文

我的应用程序中有一个 UIScrollview,我用大量大约 900 个 UIImageView 填充它。它们都非常小,并且只包含两个不同的图像。

现在,我在检测对这些 UIImageView 之一的触摸时遇到了很多麻烦。

我为它们分配了一个独特的标签,以便能够区分它们,但我真的很难检测到触摸。

目标只是能够更改触摸的 UIImageView 的图像。

由于涉及大量视图,针对每个 UIImageViews 框架检查触摸坐标的简单循环只是挂起我的应用程序。

有人有什么建议吗?

提前致谢。

I have a UIScrollview in my app and I populate it with LOTS of UIImageViews approx 900. They are all very small and consist of only two different images over and over again.

Now I am having a lot of trouble detecting a touch on one of these UIImageViews.

I have assigned them all a unique TAG so as to be able to distinguish between them but I am really struggling to detect the touch.

The goal is just to be able to change the image of the touched UIImageView.

Due to the large amount of views involved a simple loop checking touch coordinates against each UIImageViews frame is just hanging my app.

Does anyone have any suggestions?

Thanks in Advance.

Ben

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

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

发布评论

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

评论(4

清醇 2024-10-24 19:19:36

就我而言,最简单的方法是添加手势识别器:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
//Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
singleTap.cancelsTouchesInView = NO; 
[myScrollView addGestureRecognizer:singleTap];   

然后使用此方法来捕获触摸:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
  { 
    CGPoint touchPoint=[gesture locationInView:myScrollView];
  }

In my case the easiest way to do it was adding a gesture recognizer:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
//Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
singleTap.cancelsTouchesInView = NO; 
[myScrollView addGestureRecognizer:singleTap];   

Then use this method to capture the touch:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
  { 
    CGPoint touchPoint=[gesture locationInView:myScrollView];
  }
青萝楚歌 2024-10-24 19:19:36

UIImageView 默认关闭触摸处理。要将 userInteractionEnabled 设置为 YES(请参阅此处的文档 http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html

此外,如果您想知道在视图层次结构中哪个视图被点击可以使用 hitTest:withEvent: (请参阅 UIView 中的文档),这比循环遍历层次结构要快得多。

从更广泛的角度来看,我认为屏幕上同时显示 900 个 UIImageView 并不是一个好的长期策略。您是否研究过通过 CoreGraphics 绘制屏幕内容?

UIImageView has touch processing turned off by default. To change that set userInteractionEnabled to YES (see the docs here http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)

Also if you want to know which view was hit in a view hierarchy you can use hitTest:withEvent: (see docs in UIView) which will be much faster than you looping through the hierarchy.

On a wider note I don't think having 900 UIImageView's on the screen all at once is going to be a good long term strategy. Have you investigated drawing the screen's content via CoreGraphics?

泅渡 2024-10-24 19:19:36

解决这个问题的最好方法是子类化 UIScrollView,为其添加 TouchBegan 等方法。

@interface MyScroll : UIScrollView
 {

 }

The best way to solve this issue is to subclass UIScrollView, add touchesBegan etc methods to it.

@interface MyScroll : UIScrollView
 {

 }
眉目亦如画i 2024-10-24 19:19:36

您可以轻松地在 UIImageView 对象上实现此方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
      //You clicked inside the object
    }
    return [super hitTest:point withEvent:event]
}

使用返回值告诉应用程序下一个触摸响应程序(例如 uiimageview 或滚动视图)。

You could easily implement this method on the UIImageView object:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
      //You clicked inside the object
    }
    return [super hitTest:point withEvent:event]
}

Use the return to tell the application the next touch responder (such as the uiimageview or the scrollview).

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