简体   繁体   中英

How to animate imageview from off screen

I have an imageview I put onto the view controller of a circle.

I would like to start that circle from off screen and move it to the location I've put it.

What would be the best way to animate it?

Try this;

-(void)viewWillAppear {
    [UIView animateWithDuration:5
                          delay:5
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                             CGRect frame = view.frame;
                             frame.origin.y = self.view.frame.size.height-200;
                             frame.origin.x = 0;
                             view.frame = frame;
                         }completion:^(BOOL finished) {}];
}

You many animation option's [UIViewAnimationOptions]

    UIViewAnimationOptionCurveEaseInOut,
    UIViewAnimationOptionCurveLinear,
    UIViewAnimationOptionTransitionFlipFromLeft,
    UIViewAnimationOptionTransitionCurlUp,
    UIViewAnimationOptionTransitionCrossDissolve,
    UIViewAnimationOptionTransitionFlipFromTop..

You want to create your image view off-screen. Its easiest if you set it up in Interface Builder that way. you can create it on-screen, add a constraint to place it at the target location, note the value, and then change the constraint's constant so the view is moved off-screen.

Then control-drag the constraint into your view controller and create an outlet to it.

To animate the image view on-screen, change the constraint's constant value to the "on-screen" value you noted above, and then call layoutIfNeeded() on the view from inside an a UIView animateWithDuration block.

If you want to add the image view in code then you need to add it off-screen, add it as a subview, and then trigger a UIView animation to move it back on-screen. You should probably also do the positioning with constraints as outlined above, although it gets more complicated because you have to create those constraints in code.

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