开发者

Passing touch events on to subviews

开发者 https://www.devze.com 2022-12-26 10:35 出处:网络
I have a view within a UIScrollView that loads an additional subview when the user presses 开发者_StackOverflow中文版a certain area. When this additional subview is visible, I want all touch events to

I have a view within a UIScrollView that loads an additional subview when the user presses 开发者_StackOverflow中文版a certain area. When this additional subview is visible, I want all touch events to be handled by this - and not by the scrollview.

It seems like the first couple events are being handled by the subview, but then touchesCancelled is called and the scrollview takes over the touch detection.

How can I make sure that the subview gets all the events as long as the movement activity is being performed on this view?

This is my implementation on touchesMoved - which I thought would do the job...

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    CGPoint touchPt = [touch locationInView:self];

    UIView *hitView = [self hitTest:touchPt withEvent:event];
    UIView *mySubView = subviewCtrl.view;

    if(hitView == mySubView) {
        [subviewCtrl.view touchesMoved:touches withEvent:event];
    }
    else {
        NSLog(@"Outside of view...");
    }
}


The responder chain hierarchy "normally" goes from subview to superview, so you shouldn't need to do the hitTest in your superview. The problem that you are having is not that you need the superview to invoke touchesMoved on the subview, but rather that UIScrollView subverts the normal responder chain hierarchy by intercepting touch events in order to deliver a smooth scrolling experience to the user. If you don't want this behaviour, then you can disable this behaviour in the scrollView by sending it the following message:

[scrollView setDelaysContentTouches:NO];

Note that this will make sure that your subview has first crack at handling the events in question (provided that it is in fact the first responder). This can negatively impact the scrolling and zooming performance of the scrollView, however, which is probably why Apple sets it to YES by default.

0

精彩评论

暂无评论...
验证码 换一张
取 消