Draggable UIView stops publishing touchhesBegan after adding to UIScrollView

In Xcode 5.1, I created a simple test application for the iPhone:

app screenshot

Structure: scrollView -> contentView -> imageView -> image 1000 x 1000top.

And at the bottom of the application with one view, I have seven draggable user UIViews.

Drag and drop is implemented in Tile.m using methods touchesXXXX.

My problem: when I add the draggable fragment to contentViewin the ViewController.m file - I can no longer drag it:

- (void) handleTileMoved:(NSNotification*)notification {
    Tile* tile = (Tile*)notification.object;
    //return;

    if (tile.superview != _scrollView && CGRectIntersectsRect(tile.frame, _scrollView.frame)) {
        [tile removeFromSuperview];
        [_contentView addSubview:tile];
        [_contentView bringSubviewToFront:tile];
    }
}

touchesBeganno longer called for Tile, as if scrollViewmasking this event.

, UIScrollView ( GameBoard.m):

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];

    NSLog(@"%s: %hhd", __PRETTY_FUNCTION__,
          [result.superview isKindOfClass:[Tile class]]);

    self.scrollEnabled = ![result.superview isKindOfClass:[Tile class]];
    return result;
}

, 0 .

+4
3

, , , . , . , . , :

@interface LNContentView : UIView

@end

@implementation LNContentView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];

    return result == self ? nil : result;
}

@end

, , self, .

: https://github.com/LeoNatan/ios-newbie

+6

, Tile , , . , UIPanGestureRecongnizer :

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; // handle drag in pan:method
[tile addGestureRecognizer:pan];
UIPanGestureRecognizer *scrollPan = self.scrollView.panGestureRecognizer;
[scrollPan requireGestureRecognizerToFail:pan];

, , , , .

- . , , Tile, / , . , UIGestureRecognizer, .

+1

, - .

Apple

Edit:

, . :

I use UITapGestureRecognizerin the representations that I want to detect by touch. Implement the following delegate method of UITapGestureRecognizer:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

A set of touches contains all the objects (views) that received the event.

0
source

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


All Articles