开发者

How to preserve touch event after new view is added by long press

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

N开发者_开发百科ewView 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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜