Reputation: 589
My app will use a Pan Gesture to allow you to pan gesture a container view within a view controller to the right to display a menu hidden behind the container view. (similar to how the YouTube app lets the user pan his/her finger to the right to bring up the main menu OR how the default lock screen lets you pan to the right to reveal the passcode keypad)
- (IBAction)testPan:(UIPanGestureRecognizer *)sender
{
CGPoint translation = [sender translationInView:self.view];
CGPoint location = [sender locationInView:self.view];
CGPoint velocity = [sender velocityInView:self.view];
_theView.frame = CGRectMake( translation.x, 50, 300, 300);
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"All fingers are lifted");
_theView.frame = CGRectMake( 10, 50, 300, 300);
}
}
So what I'd like to do right now is make sure that this line:
_theView.frame = CGRectMake( 10, 50, 300, 300);
After the if statement happens gradually rather than instantly as it does right now.
My plan initially was to use a loop that would take the value "translation.x" and gradually through milliseconds make it equal to 10 to give the pan gesture an animated looking effect.
But apparently Objective-C doesn't allow you to sleep() for milliseconds, I'm guessing they don't allow this for efficiency purposes.
So how could I make a float value go from x to 10 within 2 seconds rather than instantly?
Does CGRect already include something to do this for you automatically to transform a shape over time?
Upvotes: 1
Views: 1067
Reputation: 71008
The exact mechanism to do what you want is built into the view system and very easy to use:
[UIView animateWithDuration:2.0 animations:^{
_theView.frame = CGRectMake(10, 50, 300, 300);
}];
You basically give UIView a duration (2 seconds) and a block inside of which you make direct changes to any animatable view properties (like frame
), which will then happen over the course of the duration. (So rather than managing a timer yourself and updating your rect and then the frame, you can just tell the system to animate the frame directly.)
Upvotes: 2