防止父视图控制器在子控制器中接收 UITouch
Prevent parent view controller from receiving UITouch in child controller
所以,在我的应用程序中,按下地图注释,它会显示一个模态视图控制器。然后,当按下模态控制器的元素时,我打开另一个较小的控制器。在两个控制器中,我都实现了方法
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (!CGRectContainsPoint([self.view viewWithTag:21].frame, touchPoint)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
这是一张显示我的布局的图片:
我的问题是,当我触摸黄色圆圈中的第一个时,第二个模态消失,然后第一个模态消失。那么,如何防止第一个模态接收到触摸。
PS: 两个模式都是透明的 UIView,里面有较小的视图来显示内容。
显然事件通过视图控制器传递给下一个响应者。
在 UIResponder 的 class 引用中。
The default implementation of this method does nothing. However
immediate UIKit subclasses of UIResponder, particularly UIView,
forward the message up the responder chain. To forward the message to
the next responder, send the message to super (the superclass
implementation); do not send the message directly to the next
responder. For example,
[super touchesMoved:touches withEvent:event];
If you override this method without calling super (a common use
pattern), you must also override the other methods for handling touch
events, if only as stub (empty) implementations.
所以如果您不想将事件转发给下一个响应者,您应该覆盖其他方法。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (!CGRectContainsPoint([self.view viewWithTag:21].frame, touchPoint)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
所以,在我的应用程序中,按下地图注释,它会显示一个模态视图控制器。然后,当按下模态控制器的元素时,我打开另一个较小的控制器。在两个控制器中,我都实现了方法
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (!CGRectContainsPoint([self.view viewWithTag:21].frame, touchPoint)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
这是一张显示我的布局的图片:
我的问题是,当我触摸黄色圆圈中的第一个时,第二个模态消失,然后第一个模态消失。那么,如何防止第一个模态接收到触摸。
PS: 两个模式都是透明的 UIView,里面有较小的视图来显示内容。
显然事件通过视图控制器传递给下一个响应者。
在 UIResponder 的 class 引用中。
The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain. To forward the message to the next responder, send the message to super (the superclass implementation); do not send the message directly to the next responder. For example,
[super touchesMoved:touches withEvent:event];
If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations.
所以如果您不想将事件转发给下一个响应者,您应该覆盖其他方法。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (!CGRectContainsPoint([self.view viewWithTag:21].frame, touchPoint)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}