iOS动画相关(持续更新)

简介:

1.When my application is entering background, because the user push the home button, the animations correctly set in pause, but when i re-open my app, the animations have disappeard.How could i fix it please ?

当我的应用进入了后台,因为用户按了home键,动画被设置成了暂停,但当我重新打开应用时,动画都消失了,我如何修复它?

This is correct and built-in behavior. When you leave the app, all animations are removed from their layers: the system calls removeAllAnimations on every layer.

你的情况是系统默认的行为.当你离开了应用后(比如进入了后台),所有的动画都从他们的layer上移除了:因为系统调用了removeAllAnimations,针对所有的layer.

附录:

UIViewController中的view显示步骤

--------------------------------------------------------------------------------------------------------

进入UIViewController时的情况:

viewDidLoad
viewWillLayoutSubviews
viewDidLayoutSubviews
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

切换了Controller后的情况(比如你在TabbarController中切换了):

viewWillDisappear
viewDidDisappear

再次切换回来后的情况:
viewWillLayoutSubviews
viewDidLayoutSubviews
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

退入到后台后的情况:

从后台进入程序时的情况:

viewWillLayoutSubviews
viewDidLayoutSubviews

--------------------------------------------------------------------------------------------------------

为了解决从后台切换回来或者从TabbarController切换回来动画还能继续动画效果,需要如下的解决方案:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
 
    // 添加通知(处理从后台进来后的情况)
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(addAnimation:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    
    // 添加动画的代码
}


- (void)addAnimation:(NSNotification *)notificaiton
{
   // 添加动画的代码
}

 

2.基本动画类型

旋转动画

/* 旋转 */
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    // 一次完整的动画所持续的时间
    animation.duration = 1.f;
    
    // 重复次数
    animation.repeatCount = HUGE_VALF;
    
    // 起始角度
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    
    // 终止角度
    animation.toValue   = [NSNumber numberWithFloat:- 2 * M_PI];
    
    // 添加动画
    [_showView.layer addAnimation:animation
                           forKey:@"rotate-layer"];

透明度
// 透明度动画
    CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    
    // 初始值
    fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
    
    // 结束值
    fadeAnim.toValue   = [NSNumber numberWithFloat:0.0];
    
    // 动画持续一次的时间
    fadeAnim.duration = 1.0;
    
    // 开始动画
    [_showView.layer addAnimation:fadeAnim forKey:@"opacity"];
    
    // 无论动画是否被中断,其最终的值还是被设置过了
    _showView.layer.opacity = 0.0;

borderWidth动画
// borderWidth动画
    CABasicAnimation *borderWidthAnimation = \
        [CABasicAnimation animationWithKeyPath:@"borderWidth"];
    
    // 初始值
    borderWidthAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    
    // 结束值
    borderWidthAnimation.toValue   = [NSNumber numberWithFloat:3.0];
    
    // 动画持续一次的时间
    borderWidthAnimation.duration    = 1.f;
    
    // 开始动画
    [_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"];
    
    // 无论动画是否被中断,其最终的值还是被设置过了
    _showView.layer.borderWidth = 3.0f;

backgroundColor动画
// backgroundColor动画
    CABasicAnimation *borderWidthAnimation = \
        [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    // 初始值
    borderWidthAnimation.fromValue = (id)[[UIColor redColor] CGColor];
    
    // 结束值
    borderWidthAnimation.toValue   = (id)[[UIColor greenColor] CGColor];
    
    // 动画持续一次的时间
    borderWidthAnimation.duration    = 1.f;
    
    // 开始动画
    [_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"];
    
    // 无论动画是否被中断,其最终的值还是被设置过了
    _showView.layer.backgroundColor = [[UIColor greenColor] CGColor];

borderColor动画
// borderColor动画
    CABasicAnimation *borderWidthAnimation = \
        [CABasicAnimation animationWithKeyPath:@"borderColor"];
    
    // 初始值
    borderWidthAnimation.fromValue = (id)[[UIColor redColor] CGColor];
    
    // 结束值
    borderWidthAnimation.toValue   = (id)[[UIColor greenColor] CGColor];
    
    // 动画持续一次的时间
    borderWidthAnimation.duration    = 1.f;
    
    // 开始动画
    [_showView.layer addAnimation:borderWidthAnimation forKey:@"borderWidth"];
    
    // 无论动画是否被中断,其最终的值还是被设置过了
    _showView.layer.backgroundColor = [[UIColor greenColor] CGColor];

 bounds.size.height动画
// bounds.size.height动画
    CABasicAnimation *borderWidthAnimation = \
        [CABasicAnimation animationWithKeyPath:@"bounds.size.height"];
    
    // 初始值
    borderWidthAnimation.fromValue = [NSNumber numberWithFloat:100.0f];
    
    // 结束值
    borderWidthAnimation.toValue   = [NSNumber numberWithFloat:300.f];
    
    // 动画持续一次的时间
    borderWidthAnimation.duration    = 0.5f;
    
    // 选择一种动画的时间轴方式
    borderWidthAnimation.timingFunction = \
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    // ??
    borderWidthAnimation.fillMode = kCAFillModeForwards;
    
    // 开始动画
    [_showView.layer addAnimation:borderWidthAnimation forKey:@"bounds.size.height"];
    
    // 无论动画是否被中断,其最终的值还是被设置过了
    _showView.layer.bounds = CGRectMake(self.view.center.x, self.view.center.y, 20, 300.f);

contents动画
// 初始化一张图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.image = [UIImage imageNamed:@"1"];
    
    // 添加进view中
    [self.view addSubview:imageView];
    
    // contents动画
    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
    
    crossFade.duration = 2.0;
    crossFade.fromValue  = (id)([UIImage imageNamed:@"1"].CGImage);
    crossFade.toValue    = (id)([UIImage imageNamed:@"2"].CGImage);
    
    [imageView.layer addAnimation:crossFade forKey:@"animateContents"];
    
    // 进行最后的设置
    imageView.image = [UIImage imageNamed:@"2"];

圆角动画
// 初始化一张图片
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.backgroundColor = [UIColor redColor];
    [self.view addSubview:showView];
    
    // 圆角动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];

    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue   = [NSNumber numberWithFloat:30.f];
    animation.duration  = 1.0;
    [showView.layer setCornerRadius:30.f];
    
    // 最后设置
    [showView.layer addAnimation:animation forKey:@"cornerRadius"];

支持的动画太多了,以下是苹果的官方文档中提出的支持的动画:

Property

Default animation

anchorPoint

Uses the default implied CABasicAnimation object, described in Table B-2.

backgroundColor

Uses the default implied CABasicAnimation object, described in Table B-2.

backgroundFilters

Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

borderColor

Uses the default implied CABasicAnimation object, described in Table B-2.

borderWidth

Uses the default implied CABasicAnimation object, described in Table B-2.

bounds

Uses the default implied CABasicAnimation object, described in Table B-2.

compositingFilter

Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

contents

Uses the default implied CABasicAnimation object, described in Table B-2.

contentsRect

Uses the default implied CABasicAnimation object, described in Table B-2.

cornerRadius

Uses the default implied CABasicAnimation object, described in Table B-2.

doubleSided

There is no default implied animation.

filters

Uses the default implied CABasicAnimation object, described in Table B-2. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.

frame

This property is not animatable. You can achieve the same results by animating the bounds and position properties.

hidden

Uses the default implied CABasicAnimation object, described in Table B-2.

mask

Uses the default implied CABasicAnimation object, described in Table B-2.

masksToBounds

Uses the default implied CABasicAnimation object, described in Table B-2.

opacity

Uses the default implied CABasicAnimation object, described in Table B-2.

position

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowColor

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowOffset

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowOpacity

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowPath

Uses the default implied CABasicAnimation object, described in Table B-2.

shadowRadius

Uses the default implied CABasicAnimation object, described in Table B-2.

sublayers

Uses the default implied CABasicAnimation object, described in Table B-2.

sublayerTransform

Uses the default implied CABasicAnimation object, described in Table B-2.

transform

Uses the default implied CABasicAnimation object, described in Table B-2.

zPosition

Uses the default implied CABasicAnimation object, described in Table B-2.

http://www.cnblogs.com/pengyingh/articles/2379631.html

目录
相关文章
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
82 1
|
编译器 iOS开发 异构计算
读iOS核心动画笔记
读iOS核心动画笔记
55 0
|
22天前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
38 1
|
1月前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
50 5
|
2月前
|
Swift iOS开发 UED
揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【9月更文挑战第5天】本文通过具体案例介绍如何在iOS应用中使用Swift与UIKit实现自定义按钮动画,当用户点击按钮时,按钮将从圆形变为椭圆形并从蓝色渐变到绿色,释放后恢复原状。文中详细展示了代码实现过程及动画平滑过渡的技巧,帮助读者提升应用的视觉体验与特色。
63 11
|
3月前
|
Swift iOS开发 UED
【绝妙创意】颠覆你的视觉体验!揭秘一款iOS应用中令人惊叹的自定义动画效果,带你领略编程艺术的魅力所在!
【8月更文挑战第13天】本文通过一个具体案例,介绍如何使用Swift与UIKit在iOS应用中创建独特的按钮动画效果。当按钮被按下时,其形状从圆形变化为椭圆形,颜色则从蓝色渐变为绿色;释放后,动画反向恢复原状。利用UIView动画方法及弹簧动画效果,实现了平滑自然的过渡。通过调整参数,开发者可以进一步优化动画体验,增强应用的互动性和视觉吸引力。
52 7
|
iOS开发
iOS 常用阅读软件打开书籍的转场动画
iOS 常用阅读软件打开书籍的转场动画
93 0
|
6月前
|
iOS开发
iOS设备功能和框架: 如何使用 Core Animation 创建动画效果?
iOS设备功能和框架: 如何使用 Core Animation 创建动画效果?
137 0
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
96 0
|
iOS开发
iOS - 个人中心果冻弹性下拉动画
iOS - 个人中心果冻弹性下拉动画
261 0
iOS - 个人中心果冻弹性下拉动画
下一篇
无影云桌面