[英]Cancel UITouch Events When View Covered By Modal UIViewController
I am writing an application where the user has to move some stuff on the screen using his fingers and drop them. 我正在编写一个应用程序,用户必须用手指在屏幕上移动一些东西并将其放下。 To do this, I am using the touchesBegan,touchesEnded... function of each view that has to be moved. 为此,我使用了每个必须移动的视图的touchesBegan,touchesEnded ...函数。
The problem is that sometimes the views are covered by a view displayed using the [UIViewController presentModalViewController] function. 问题是,有时视图被[UIViewController presentModalViewController]函数显示的视图覆盖。 As soon as that happens, the UIView that I was moving stops receiving the touch events, since it was covered up. 一旦发生这种情况,我正在移动的UIView就会停止接收触摸事件,因为它被掩盖了。 But there is no event telling me that it stopped receiving the events, so I can reset the state of the moved view. 但是没有事件告诉我它停止接收事件,因此我可以重置移动视图的状态。
The following is an example that demonstrates this. 下面是一个演示此示例。 The functions are part of a UIView that is being shown in the main window. 这些功能是在主窗口中显示的UIView的一部分。 It listens to touch events and when I drag the finger for some distance, it presents a modal view that covers everything. 它侦听触摸事件,当我拖动手指一定距离时,它会呈现出覆盖所有内容的模式视图。 In the Run Log, it prints what touch events are received. 在运行日志中,它打印接收到的触摸事件。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesBegan");
touchStart=[[touches anyObject] locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchAt=[[touches anyObject] locationInView:self];
float xx=(touchAt.x-touchStart.x)*(touchAt.x-touchStart.x);
float yy=(touchAt.y-touchStart.y)*(touchAt.y-touchStart.y);
float rr=xx+yy;
NSLog(@"touchesMoved %f",rr);
if(rr > 100) {
NSLog(@"Show modal");
[viewController presentModalViewController:[UIViewController new] animated:NO];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesCancelled");
}
But when I test the application and trigger the modal dialog to be displayed, the following is the output in the Run Log. 但是,当我测试应用程序并触发要显示的模态对话框时,以下是“运行日志”中的输出。
[Session started at 2010-03-27 16:17:14 -0700.] 2010-03-27 16:17:18.831 modelTouchCancel[2594:207] touchesBegan 2010-03-27 16:17:19.485 modelTouchCancel[2594:207] touchesMoved 2.000000 2010-03-27 16:17:19.504 modelTouchCancel[2594:207] touchesMoved 4.000000 2010-03-27 16:17:19.523 modelTouchCancel[2594:207] touchesMoved 16.000000 2010-03-27 16:17:19.538 modelTouchCancel[2594:207] touchesMoved 26.000000 2010-03-27 16:17:19.596 modelTouchCancel[2594:207] touchesMoved 68.000000 2010-03-27 16:17:19.624 modelTouchCancel[2594:207] touchesMoved 85.000000 2010-03-27 16:17:19.640 modelTouchCancel[2594:207] touchesMoved 125.000000 2010-03-27 16:17:19.641 modelTouchCancel[2594:207] Show modal [会议开始于2010-03-27 16:17:14 -0700。] 2010-03-27 16:17:18.831 modelTouchCancel [2594:207] touchesBegan 2010-03-27 16:17:19.485 modelTouchCancel [2594:207 ] touchesMoved 2.000000 2010-03-27 16:17:19.504 modelTouchCancel [2594:207] touchesMoved 4.000000 2010-03-27 16:17:19.523 modelTouchCancel [2594:207] touchesMoved 16.000000 2010-03-27 16:17:19.538 modelTouchCancel [2594:207] touchesMoved 26.000000 2010-03-27 16:17:19.596 modelTouchCancel [2594:207] touchesMoved 68.000000 2010-03-27 16:17:19.624 modelTouchCancel [2594:207] touchesMoved 85.000000 2010-03-27 16:16 17:19.640 modelTouchCancel [2594:207] touchesMoved 125.000000 2010-03-27 16:17:19.641 modelTouchCancel [2594:207]显示模式
Any suggestions on how to reset the state of a UIView when its touch events are interrupted by a modal view? 关于在模式视图中断UIView的触摸事件时如何重置UIView的任何建议?
如果您要控制何时显示模式视图,是否还可以同时发送通知以告知应用程序的其余部分,他们应该重置移动的视图?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.