制作飞行动画

时间:2014-03-25 04:42:39

标签: ios iphone objective-c core-animation

我希望有一种“飞入”PowerPoint,就像在Xcode中完成动画一样。

视图将从给定方向(向上,向下,向左,向右)飞入屏幕中心停留给定时间,然后继续以相同方向飞行,直到它离开屏幕< / p>

我尝试过使用不同的动画选项,但都表现得像:

UIViewAnimationOptionTransitionNone

所以我做错了什么?

UIViewAnimationOptions animationOption[] = {
UIViewAnimationOptionTransitionNone,
UIViewAnimationOptionTransitionFlipFromLeft,
UIViewAnimationOptionTransitionFlipFromRight,
UIViewAnimationOptionTransitionCurlUp,
UIViewAnimationOptionTransitionCurlDown,
UIViewAnimationOptionTransitionCrossDissolve,
UIViewAnimationOptionTransitionFlipFromTop,
UIViewAnimationOptionTransitionFlipFromBottom
};

self.frame = p_newFrame;
int idx = arc4random() % 8;
[UIView animateWithDuration:p_duration delay:0.8 options:animationOption[idx] animations:^{
      self.alpha = 1.0;
} completion:^(BOOL finished) {
}];

任何人都可以帮助代码示例吗?

1 个答案:

答案 0 :(得分:1)

有很多方法可以实现这一点,但一种简单的方法是添加一个子视图,设置初始frame,使其最初在屏幕外。然后,设置frame的更改动画,使其在可见屏幕内。然后,在完成块中,使另一个动画(这个具有延迟的动画)使其从另一个方向飞行。 E.g。

CGRect frameVisible = self.view.bounds;                                // Or use `CGRectMake` to specify something smaller than the whole screen
CGRect frameRight = frameVisible;
frameRight.origin.x += self.view.frame.size.width;
CGRect frameLeft = frameVisible;
frameLeft.origin.x -= self.view.frame.size.width;

UIView *subview = [[UIView alloc] initWithFrame:frameRight];           // add if off screen to right

// just doing this so I can see it; you'd presumably add all sorts of subviews
// (labels, images, whatever)

subview.backgroundColor = [UIColor lightGrayColor];                    // I'm just going to make it gray, so I can see it

[self.view addSubview:subview];

[UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
    subview.frame = frameVisible;                                      // animate it on screen
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.5 delay:3.0 options:0 animations:^{  // wait 3 sec, then ...
        subview.frame = frameLeft;                                     // ... animate it off screen to left
    } completion:^(BOOL finished) {
        [subview removeFromSuperview];                                 // when all done, remove it from screen
    }];
}];

只需调整您用于CGRect属性的各种frame值,即可控制它的启动位置,屏幕停止位置以及它飞往的位置。