简体   繁体   中英

Core Animation Off-Screen to Onscreen Animation

In the app I'm making, I'm trying to make a button start offscreen and move onscreen. I'm not sure how to start with the button offscreen, but I know that once I figure that out, I can do something like the code below:

[UIView beginAnimation:@"animate" context: nil];
[UIView setAnimationDuration:3];
self.myButton.frame = CGRectMake (20, 100, 40, 80);
//Other animations
[UIView commitAnimations];

Additionally, in the line [UIView beginAnimation:@"animate" context:nil]; , is the context parameter asking for a CGContextRef?

I assume that you are asking how to get the button to animate from offscreen to onscreen, to do this just set the buttons origin position to coordinates that are located offscreen.

self.myButton.frame = CGRectMake(-80, 100, 40, 80);

Once you have done this you can just use the code you posted to animate onto the screen. Keep in mind that the button will move from the first position to the second, meaning that if you used the coordinates i used the button would move onto the screen from the left side without the y position changing. Keep in mind that the further offscreen you put the button the faster it will have to move to get onto the screen in the time allotted in the setAnimationDuration.

So to animate onto screen from the left

self.myButton.frame = CGRectMake(-80, 100, 40, 80);
// begin animation block    
[UIView beginAnimations:@"animate" context: nil];
[UIView setAnimationDuration:3];
self.myButton.frame = CGRectMake (20, 100, 40, 80);
// commit frame changes to be animated
[UIView commitAnimations];

Also, if the button was previously on the screen it will seem to teleport off the screen and then slide back on when that code is used.

Oh, and to answer your question about the context, no it doesn't have to be a CGContextRef, it can be any data type (as long as its an object and not a primitive). Its basically the data that gets passed to the delegate when the animation occurs.

BeginAnimation:context: is discouraged in iOS 4.0 or later, you should use one of the block based methods. You should set the original frame so that it is off screen. Here is an example that moves the button in from the right after a slight delay:

self.myButton.frame = CGRectMake (340, 250, 40, 80);
    [self.view addSubview:myButton];
    [UIView animateWithDuration:5 delay:.2 options: UIViewAnimationOptionLayoutSubviews animations:^{
    self.myButton.frame = CGRectMake (140, 250, 40, 80);
    //Other animations
    }
     completion:nil];

If you're not using xibs, you can just set the frame of the button offscreen in the viewDidLoad (If you are using xibs, slide it off the view):

 //create the button or get it from the xib
 CGRect rect = self.myButton.frame;
 rect.origin.x=-200;//or a good point to hide the button
 self.myButton.frame=rect;

then when you want to animate the button you could use a block animation:

 [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.myButton.frame=SET_YOUR_FRAME;
    } completion:^(BOOL finished)];

For the context see the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM