简体   繁体   English

覆盖的UIControl或UIView如何知道结束于其上方的触摸事件?

[英]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. 我有一个UIControl(或无关紧要的UIView),这由另一个UIControl覆盖。 The other UIControl reacts nicely on touch events. 另一个UIControl对触摸事件反应良好。 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. 但是,从用户的角度来看,底层的UIControl还需要了解触摸以及它是否真正在其上。 The covering UIControl is partially transparent. 覆盖的UIControl是部分透明的。

How can I catch this touch on the underlying UIControl? 我如何在底层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? 您可以将touch事件传递给另一个控件...尽管我不认为将上方视图移到另一个视图上会起作用? 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 或者您可以使用-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event来询问视图本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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