如何通过长按添加新视图后保存触摸事件 [英] How to preserve touch event after new view is added by long press

查看:251
本文介绍了如何通过长按添加新视图后保存触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检测到用户的长按之后,当我添加一个新的视图时,我会得到touchesCancelled事件。
但是,我想保留新添加的新闻视图。



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



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



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

以下是日志消息。


MyView touchesBegan x:524 y:854



MyView handleLongPress(检测到LongPress)



添加新视图



MyView touchesCancelled x:526 y:854



没有发生...


我期待的是...


MyView touchesBegan x:524 y:854



MyView handleLongPress(检测到LongPress)



添加新视图



MyView touchesCancelled x: 526 y:854



NewView touchBegan



NewView touchMoved



NewView touchMoved



NewView touchMoved



NewView touchMoved



...


有没有解决方案?



提前感谢

解决方案

这是一个棘手的一个 - 我的解决方案的想法有点黑客,但我认为它会工作。



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



如果您需要检测其他触摸,例如在可见视图中的按钮和其他UI元素中,则应创建一个 UIView InterceptorView )为您的拦截器视图和覆盖 hitTest:withEvent:如下:

   - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
//注意:
// visibleView是您的InterceptorView类
//的属性,当拦截器
//视图创建并添加到可见的顶部时,该属性应设置为可见视图视图。

//查看可见视图中是否有任何应该接收到的视图...
//因为拦截器视图的框架应该与
//可见视图然后点不需要坐标转换。
UIView * passThroughView = [self.visibleView hitTest:point withEvent:event];
if(passThroughView == nil)
{
//可见视图及其子视图不想接收此触摸
//这意味着它是安全的我拦截它
返回自我;
}
//可见视图想要这个触摸,所以告诉系统我不想要它。
return nil;
}

这将意味着您的拦截器视图将处理长按,除非新闻是可见视图的互动部分,在这种情况下,它将允许触摸传递到可见视图及其子视图。



我避风港 t测试这个,这只是一个想法,所以请让我知道你如何得到它:)


When I add a new view after detecting user's long press, I get touchesCancelled event. However, I want to preserve the long press event to newly added view.

What I want to implement is user touch & hold the screen, then new view added, and user can move touch around in the newly added view without touch up and touch down again.

But, when new view is added, I get touch cancel event, so the added view can not receive any touch event even user's touch is moving.

I'm using UILongPressGestureRecognizer to detect user's long press.

below is log message.

MyView touchesBegan x:524 y:854

MyView handleLongPress (LongPress Detected)

NewView added

MyView touchesCancelled x:526 y:854

and nothing happend...

what I'm expecting is...

MyView touchesBegan x:524 y:854

MyView handleLongPress (LongPress Detected)

NewView added

MyView touchesCancelled x:526 y:854

NewView touchBegan

NewView touchMoved

NewView touchMoved

NewView touchMoved

NewView touchMoved

...

Is there any solution?

Thanks in advance.

解决方案

This is a tricky one - my idea for a solution is a little bit hacky, but I think it will work.

Add a transparent view over the entire area and this is the view that you add your long-press gesture recognizer. I will call this the interceptor view and the one behind will be called the visible view. When you detect your long press in the interceptor view you can add the new view to the visible view without interfering with the touches on the interceptor view, therefore you can track them and move the new view around on the visible view.

If you need to detect other touches, for example in buttons and other UI elements that are in the visible view, then you should create a subclass of UIView (InterceptorView) for your interceptor view and override hitTest:withEvent: as follows:

- (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;
}

This will mean that your interceptor view will handle the long press, except when the press is over an interactive part of the visible view, in that case it will allow the touch to pass through to the visible view and its sub-views.

I haven't tested this, it's just an idea, so please let me know how you get on with it :)

这篇关于如何通过长按添加新视图后保存触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆