ios - 创建飞行动画

标签 ios iphone objective-c core-animation

我想要一种在 Xcode 中完成的类似“飞入”PowerPoint 的动画。

View 将从给定方向(上、下、左、右)飞入,在屏幕中心停留给定的时间,然后继续沿同一方向飞行,直到飞出屏幕

我尝试过使用不同的动画选项,但所有的行为都像:

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) {
}];

任何人都可以帮忙编写代码示例吗?

最佳答案

有很多方法可以实现这一点,但一个简单的技术是添加一个 subview ,设置初始 frame 使其最初处于屏幕外。然后,为 frame 的变化设置动画,使其位于可见屏幕内。然后,在完成 block 中,使用另一个动画(这个有延迟的动画)来动画它从另一个方向飞走。例如

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
    }];
}];

只需调整您将用于 frame 属性的各种 CGRect 值,即可控制它的开始位置、在屏幕上的停止位置以及飞到的位置。

关于ios - 创建飞行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22625599/

相关文章:

iphone - NSDate 日期返回的时间比应有的少 2 小时

ios - 从 prepare segue 获取数据后将值字典添加到标签

iphone - iOS - 管理 IBOutlets 内存的最佳方式是什么?

iphone - 具有 iOS 3.2 的 iPad 与 iPhone iOS 3.0 的兼容性

iphone - 从后台线程访问 NSManagedObject

objective-c - IOS FacebooSDK 3.0 模式 viewController 中的 FBLoginVIew

objective-c - 将 UI Picker 添加到 UITextView

ios - 如何在 ios 中的单个 View Controller 中的两个 UITableView 中工作

ios - 更改 UISearchBar 搜索字段背景

iphone - 我如何完全杀死一个 View Controller ?