Reputation: 41
I want to make a button that is outside the view at first but drops down into the viewable screen when I hold the phone upside down. How can I code this? Thanks in advance!
Upvotes: 0
Views: 236
Reputation: 8664
You want to use Core animation by using an UIView method call.
animateWithDuration:delay:options:animations:completion:
You can use one with less parameter if you want the default animation style. But basically you specify the time in the method and in the block you just set the position of you button to where you want it and core animation will take care of the details.
The code may look like this one :
-(IBAction)moveIt:(id)sender {
__block CGPoint originalPoint = self.aButton.center;
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAutoreverse
animations:^{
self.aButton.center = CGPointMake(200, 200);
}
completion:^(BOOL finished) {
self.aButton.center = originalPoint;
}];
}
What does it do? This code is moving a UIButton from its original place to an arbitrary point and back to it's original point (the UIViewAnimationOptionAutoreverse
option).
Note that you have to set back the UIButton to it's originalPoint
at the end because of the autoreverse, if you don't do theautoreverse
you don't need to that at the end.
Upvotes: 1