How to detect a touch of a UIImageView inside a UIScrollview?

I have a UIScrollview in my application, and I fill it with LOTS UIImageViews of about 900. They are all very small and consist of two different images over and over again.

Now I have a lot of trouble detecting the touch of one of these UIImageViews.

I assigned them an all-unique TAG to be able to distinguish between them, but I'm really trying to detect the touch.

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

Due to the large number of views, a simple cycle of checking the coordinates of the touch for each frame of UIImageViews is involved, my application just hangs.

Does anyone have any suggestions?

Thanks at Advance.

Ben

+3
4

- UIScrollView, touchhesBegan ..

@interface MyScroll : UIScrollView
 {

 }
0

- :

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];
  }
+4

UIImageView . userInteractionEnabled YES (. http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImageView_Class/Reference/Reference.html)

, , , hitTest:withEvent: (. docs UIView), , .

In a wider note, I do not think that having 900 UIImageView on the screen will immediately become a good long-term strategy. Have you explored drawing screen contents through CoreGraphics?

+3
source

You can easily implement this method in a 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 return to tell the app the next touch on the responder (for example, uiimageview or scrolling).

0
source

Source: https://habr.com/ru/post/1793048/


All Articles