Josh O'Connor
Josh O'Connor

Reputation: 4962

Pass touch to another view controller (iOS)

I have a long press gesture which presents another controller. In the presented controller, I have touchesBegan(), touchesMoved(), and touchedEnded() methods which detect touching on the view controller.

When I long press, and hold down, the next view controller is presented. If I do not release the long press, and move my finger on the screen, the touchesMoved() method does not get called, and when I stop touching, the touchesEnded() is not called. However, when I release the long press touch, and touch it again, these methods will be called, since the long press gesture recognizer is no longer receiving the touch, and rather the presented view controller receives the touches.

Is there anyway for the presented screen to recognize the touches, while still pressing the long press? How do I pass this long press recognizer to the next view controller?

Upvotes: 0

Views: 871

Answers (1)

Andrew McKinley
Andrew McKinley

Reputation: 1137

The gesture recognizer exist on a view. When you present another view controller that view has disappeared temporarily. Its disappearance is causing this problem. The solution is to not actually present another view controller but to feign it. Picture your first view on your first view controller (we'll call this view1). Create another view of the exact size and put it on top (we'll call this view2). Put the gesture recognizer on view2. The user long presses the screen. Even though view2 got the touches you can find out where the user touched in view1 as follows:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    if let touch = touches.first {
        let position :CGPoint = touch.locationInView(view1)
    }
}

At this point you can animate in view3 behind view2 and in front of view1. This will give the effect of going to a new screen, however the view receiving the touch commands never went anywhere and is still listening to commands.

Upvotes: 2

Related Questions