简体   繁体   中英

How can a covered UIControl or UIView know about a touch event that ended above it?

I have a UIControl (or UIView, doesn't matter which) and this is covered by another UIControl. The other UIControl reacts nicely on touch events. However, the underlying UIControl also needs to know about the touch and if it was actually "on it" or not from the user's perspective. The covering UIControl is partially transparent.

How can I catch this touch on the underlying UIControl?

I think there are couple ways you could go about this...

You could pass the touch event on to the other control...though i don't think that'll work if you're moving the upper view over the other view? You might have to experiment.

The easier way might be just to see if the lower rect contains the touch point:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    if (CGRectContainsPoint(lowerView.frame, touchLocation)) {
        <doyourthing>
}

I don't recall offhand but you may need to convert the view coordinates between the two views?!? or you could ask the view itself with - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM