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 ?html

當個人應用進入了後臺,由於用戶按了home鍵,動畫被設置成了暫停,但當我從新打開應用時,動畫都消失了,我如何修復它?ios

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.app

你的狀況是系統默認的行爲.當你離開了應用後(好比進入了後臺),全部的動畫都從他們的layer上移除了:由於系統調用了removeAllAnimations,針對全部的layer.ide

附錄:動畫

UIViewController中的view顯示步驟ui

--------------------------------------------------------------------------------------------------------spa

進入UIViewController時的狀況:code

viewDidLoad
viewWillLayoutSubviews
viewDidLayoutSubviews
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
orm

切換了Controller後的狀況(好比你在TabbarController中切換了):server

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

相關文章
相關標籤/搜索