如何在长按添加新视图后保留触摸事件

时间:2010-10-27 09:08:52

标签: iphone events ipad ios touch

当我在检测到用户长按后添加新视图时,我会收到touchesCancelled事件。 但是,我想将长按事件保存到新添加的视图中。

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

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

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

下面是日志消息。

  

MyView touchesBegan x:524 y:854

     

MyView handleLongPress(检测到LongPress)

     

NewView添加

     

MyView touchesCancelled x:526 y:854

     

并没有发生任何事......

我期待的是......

  

MyView touchesBegan x:524 y:854

     

MyView handleLongPress(检测到LongPress)

     

NewView添加

     

MyView touchesCancelled x:526 y:854

     

NewView touchBegan

     

NewView touchMoved

     

NewView touchMoved

     

NewView touchMoved

     

NewView touchMoved

     

...

有没有解决方案?

提前致谢。

1 个答案:

答案 0 :(得分: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;
}

这意味着您的拦截器视图将处理长按,除非印刷机位于可见视图的交互式部分上,在这种情况下,它将允许触摸传递到可见视图及其子视图

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

相关问题