Reputation: 10759
I have a view in my app that takes up the bottom half the screen. I want to allow the user to drag the view down so it only takes up 1/4 of the bottom of the screen. The user can then drag it back up to take up 1/2 the screen again. So like the bottom slide up view in iOS7 (i.e. flashlight, calculator, volume, etc...). I need to function in the exact same manner, but just not let it go all the way off the screen.
So I need to keep the pan vertical only. I also want it to stop when it covers half the screen then stop the other direction when it is 1/4 of the screen. I would also like it to snap into place when dragged past a certain point so that the only two positions are 1/2 on and 1/4 on screen. So I don't want the user to be able to drag it 1/3 on the screen.
Here is what I have so far, but it allows to me to fling or throw the view past the max stopping point.
- (IBAction)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint vel = [gestureRecognizer velocityInView:piece];
if (vel.y > 0)
{
// user dragged towards the right
//counter++;
if(piece.frame.origin.y >= (self.view.frame.size.height - piece.frame.size.height) ) {
// don't update
} else {
[piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
}
} else {
// user dragged towards the left
//counter--;
NSLog(@"");
if(piece.frame.origin.y <= 205) {
// don't update
} else {
[piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
}
}
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
Upvotes: 0
Views: 969
Reputation: 488
Take a look at the UIResponder documentation and touch gestures in this sample project: https://developer.apple.com/library/ios/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435
Upvotes: 2