alionthego
alionthego

Reputation: 9723

how can i end a UIPanGesture when the user's finger is no longer in the view where the panGesture was initiated?

i'm fairly new iOS programming and swift.

i can't figure out how to force my panGesture to stop when the user's finger is no longer in the view in which the pan gesture was initiated from.

At the moment the pan is correctly initiated when the user presses down on that view and drags. But it continues until the user lift's their finger from anywhere on the display. I would like the pan to end when the user's finger is no longer in that view or the user lifts their finger.

Any help would be greatly appreciated. Thank you

Upvotes: 0

Views: 137

Answers (2)

alionthego
alionthego

Reputation: 9723

based on the helpful posts above I managed to come up with code which works for me.

if CGRectContainsPoint(initiatedView.frame, gesture.locationInView(initiatedView))
{
    // do stuff
} else {
    gesture.enabled = false
}

Upvotes: 0

Daniel T-D
Daniel T-D

Reputation: 675

I believe this method is called throughout the pan gesture. You'll just have to keep a reference to the view that the pan gesture was initiated in.

- (IBAction)handlePan:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint panPoint = [panRecognizer locationInView:self.view];

    if(CGRectContainsPoint(initiatedView, panPoint))
    {
        // Carry on with pan behaviour
    }
    else
    {
        // Do nothing
    }
}

Reference for CGRectContainsPoint()

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGRectContainsPoint

Upvotes: 2

Related Questions