长按新增视图后如何保存触摸事件

当我检测到用户长按后添加一个新的视图,我得到了touchesCledlled事件。 但是,我想保留长按事件到新添加的视图。

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

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

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

下面是日志消息。

MyView touchesBegan x:524 y:854

MyView handleLongPress(检测到LongPress)

NewView添加

MyView触摸取消x:526 y:854

什么也没发生

我期待的是…

MyView touchesBegan x:524 y:854

MyView handleLongPress(检测到LongPress)

NewView添加

MyView触摸取消x:526 y:854

NewView touchBegan

NewView touchMoved

NewView touchMoved

NewView touchMoved

NewView touchMoved

有没有解决办法?

提前致谢。

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

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

如果需要检测其他触摸,例如在可见视图中的button和其他UI元素中,则应该为拦截器视图创buildUIViewInterceptorView )的子类,并重写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; } 

这意味着你的拦截器视图将处理长按,除非当新闻结束了可见视图的交互部分,在这种情况下,它将允许触摸传递给可见视图及其子视图。

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