Vayn
Vayn

Reputation: 2717

How to limit area when sliding a view with UIPanGestureRecognizer?

I have setup a pan gesture to the slide view, I want to prevent the view sliding if it has been slid out. So I add this when I check UIGestureRecognizerStateChanged

CGPoint velocity = [(UIPanGestureRecognizer *)sender velocityInView:self.view];

if (sender.state == UIGestureRecognizerStateChanged) {
    CGPoint velocity = [(UIPanGestureRecognizer *)sender velocityInView:self.view];

    if (self.slideMenuView.frame.origin.x > 0 && velocity > 0) {
        return;
    }

    // ...
}

It seems working, but if I slide the view to left then drag it to the opposite direction very quickly, the check will become invalid:

screencast

Please help me out.

UPDATE: I have uploaded the project to GitHub: Vayn/ice

Upvotes: 0

Views: 164

Answers (2)

Vayn
Vayn

Reputation: 2717

With help from @PhilCai1993, we found solution for this problem finally:

if (sender.state == UIGestureRecognizerStateChanged) {
    // ...

    // Add the check at the end of the block
    if (sender.view.frame.origin.x >= 0) {
        sender.view.frame = CGRectMake(0, sender.view.frame.origin.y,
                                       sender.view.frame.size.width, sender.view.frame.size.height);
    }
}

Just checking the position of slide menu view at the end of the block after updating the frame of slide menu view.

Thanks @anhtu for helping me solve this problem!

Upvotes: 1

tuledev
tuledev

Reputation: 10327

Can you try it.

if (self.view.frame.origin.x > 0) {
    sender.view.frame = CGRectMake(0, sender.view.frame.origin.y,
                                   sender.view.frame.size.width, sender.view.frame.size.height);
    return;
}

I'm not sure. I downloaded your project. But can't build

library not found for -lSDCycleScrollView

Upvotes: 1

Related Questions