我正在编写一个应用程序,用户必须用手指在屏幕上移动一些东西并放下它们。为此,我使用了必须移动的每个视图的 touchesBegan,touchesEnded... 功能。
问题是有时视图被使用 [UIViewController presentModalViewController] 函数显示的视图覆盖。一旦发生这种情况,我移动的 UIView 就会停止接收触摸事件,因为它被掩盖了。但是没有事件告诉我它停止接收事件,所以我可以重置移动视图的状态。
下面是一个例子来说明这一点。这些函数是主窗口中显示的 UIView 的一部分。它监听触摸事件,当我将手指拖动一段距离时,它会呈现一个覆盖所有内容的模态视图。在运行日志中,它打印接收到的触摸事件。
- (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");
}
但是当我测试应用程序并触发要显示的模态对话框时,运行日志中的输出如下。
[会话开始于 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 模型TouchCancel[2594:207] touchesMoved 4.000000 2010-03-27 16:17:19.523 模型TouchCancel[2594:207] touchesMoved 16.00000016:17:19.53-278 模型TouchCancel [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:0100-85.03-20000-016-6 17:19.640 modelTouchCancel[2594:207] touchesMoved 125.000000 2010-03-27 16:17:19.641 modelTouchCancel[2594:207] 显示模态
当 UIView 的触摸事件被模态视图中断时如何重置 UIView 的状态有什么建议吗?