2010-10-27 69 views
2

当我在检测到用户的长按后添加新视图时,我得到touchesCancelled事件。 但是,我想将长按事件保存到新添加的视图。如何通过长按添加新视图后保留触摸事件

我想要实现的是用户触摸&保持屏幕,然后添加新视图,并且用户可以在新添加的视图中移动触摸而无需触摸并再次触摸。

但是,当添加新视图时,我会收到触摸取消事件,因此即使用户的触摸正在移动,添加的视图也不会收到任何触摸事件。

我使用UILongPressGestureRecognizer来检测用户的长按。

以下是日志消息。

MyView的的touchesBegan X:524 Y:854个

MyView的handleLongPress(长按检测)

NewView的加入

MyView的touchesCancelled X:526 Y:854

并没有什么happend。 ..

wh在我期待的...

MyView的的touchesBegan X:524 Y:854个

MyView的handleLongPress(长按检测)

NewView的加入

MyView的touchesCancelled X:526 Y: 854

NewView的touchBegan

NewView的touchMoved

NewView的touchMoved

NewView的touchMoved

NewView的touchMoved

...

有没有什么解决办法吗?

在此先感谢。

回答

0

这是一个棘手的问题 - 我对解决方案的想法有点冒失,但我认为它会起作用。

在整个区域添加透明视图,这是您添加长按手势识别器的视图。我会称之为拦截器视图,后面的视图将被称为可见视图。当您在拦截器视图中检测到长按时,您可以将新视图添加到可见视图中,而不会干扰拦截器视图上的触摸,因此可以跟踪它们并在可见视图上移动新视图。

如果需要检测其它触摸,例如按钮和在可见视图中的其他UI元素,那么您应该创建的UIViewInterceptorView)的子类为您拦截器视图,并覆盖hitTest:withEvent:如下:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event 
{ 
    // Notes: 
    // visibleView is a property of your InterceptorView class 
    // which should be set to the visible view when the interceptor 
    // view is created and added over the top of the visible view. 

    // See if there are any views in the visible view that should receive touches... 
    // Since the frame of the interceptor view should be the same as the frame of the 
    // visible view then the point doesn't need coordinate conversion. 
    UIView* passThroughView = [self.visibleView hitTest:point withEvent:event]; 
    if(passThroughView == nil) 
    { 
     // The visible view and its sub-views don't want to receive this touch 
     // which means it is safe for me to intercept it. 
     return self; 
    } 
    // The visible view wants this touch, so tell the system I don't want it. 
    return nil; 
} 

这将意味着您的拦截器视图将处理长按,除非当按钮位于可见视图的交互部分时,在这种情况下,它将允许触摸传递到可见视图及其子-views。

我还没有测试过这个,这只是一个想法,所以请让我知道你如何继续:)