您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 動畫實作整理:UIView 動畫、Core Animation 與翻轉效果

編輯:iOS 開發綜合

本文整理 iOS 中常見的動畫寫法,從傳統的 UIView beginAnimations 到 block 寫法,再到 Core Animation 的圖層動畫與轉場效果,方便日後查閱。

傳統 UIView 動畫

// 開始動畫
[UIView beginAnimations:nil context:nil];
// 動畫時長
[UIView setAnimationDuration:2.f];
// 延遲啟動
[UIView setAnimationDelay:2];
// 速度曲線
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// 設定代理
[UIView setAnimationDelegate:self];
// 開始時呼叫的代理方法
[UIView setAnimationWillStartSelector:@selector(start)];
// 結束時呼叫的代理方法
[UIView setAnimationDidStopSelector:@selector(stop)];
// 是否自動往返
[UIView setAnimationRepeatAutoreverses:YES];
// 重複次數
[UIView setAnimationRepeatCount:2];
// 翻轉效果
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.Testview cache:YES];
// 動畫過程中變更屬性
self.Testview.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];
// 提交動畫
[UIView commitAnimations];

系統提供的 block 寫法

[UIView animateWithDuration:2.f animations:^{
    // 在此變更要動畫的屬性
}];

帶轉場的動畫

[UIView transitionWithView:self.view duration:2.f options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
    NSLog(@"開始動畫");
    self.MyView.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
    self.MyView.backgroundColor = [UIColor orangeColor];
    NSLog(@"動畫結束");
}];

使用 CGAffineTransform 做變形

[UIView animateWithDuration:2.f animations:^{
    // 平移
    // self.MyView.transform = CGAffineTransformTranslate(self.MyView.transform, 0.5f, 0.5f);
    // 旋轉
    self.MyView.transform = CGAffineTransformRotate(self.MyView.transform, M_PI_2);
}];

封裝自己的動畫方法

// 模擬系統 block,自訂動畫方法
-(void)myAnimationDuration:(NSTimeInterval)time animation:(myBlock)ani {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:time];
    ani();
    [UIView commitAnimations];
}

為 view 設定邊框與陰影

// 邊框粗細
self.myview.layer.borderWidth = 20;
// 邊框顏色(注意型別轉換)
self.myview.layer.borderColor = [UIColor yellowColor].CGColor;
// 陰影偏移
self.myview.layer.shadowOffset = CGSizeMake(50, 50);
// 陰影顏色
self.myview.layer.shadowColor = [UIColor grayColor].CGColor;
// 陰影透明度
self.myview.layer.shadowOpacity = 0.8;
// 陰影模糊程度
self.myview.layer.shadowRadius = 10;

Core Animation 圖層動畫

第一種:CAKeyframeAnimation 沿多個關鍵點移動。

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 2.0;
animation.repeatCount = 30;
animation.autoreverses = YES;
animation.values = @[[NSValue valueWithCGPoint:CGPointMake(25, 25)],
                     [NSValue valueWithCGPoint:CGPointMake(200, 25)],
                     [NSValue valueWithCGPoint:CGPointMake(25, 200)],
                     [NSValue valueWithCGPoint:CGPointMake(200, 200)]];
[self.myview.layer addAnimation:animation forKey:@"asd"];

第二種:CAAnimationGroup 組合多個動畫。

CAAnimationGroup *group = [CAAnimationGroup animation];
CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation1.fromValue = [NSNumber numberWithFloat:0.3];
animation1.toValue = [NSNumber numberWithFloat:3.0];
CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation2.values = @[[NSValue valueWithCGPoint:CGPointMake(25, 25)],
                      [NSValue valueWithCGPoint:CGPointMake(200, 25)],
                      [NSValue valueWithCGPoint:CGPointMake(25, 200)],
                      [NSValue valueWithCGPoint:CGPointMake(200, 200)]];
group.duration = 2.0;
group.autoreverses = YES;
group.repeatCount = 3;
group.animations = @[animation1, animation2];
[self.myview.layer addAnimation:group forKey:@"asd"];

第三種:CATransition 轉場效果。

CATransition *ani = [CATransition animation];
/* type 可選值:
   pageCurl    向上翻頁
   pageUnCurl  向下翻頁
   rippleEffect 滴水效果
   suckEffect  收縮效果(如布被抽走)
   cube        立方體效果
   oglFlip     上下翻轉效果 */
ani.type = @"cube";
ani.repeatCount = 200;
ani.duration = 1;
ani.subtype = kCATransitionFromLeft;
[self.myview.layer addAnimation:ani forKey:@"an"];
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved