iOS簡(jiǎn)單動(dòng)畫

知識(shí)架構(gòu)


1、CALayer 圖層類

2、CABasicAnimation 基礎(chǔ)動(dòng)畫

3、CAKeyFrameAnimation 幀動(dòng)畫

4、CATransition 轉(zhuǎn)場(chǎng)動(dòng)畫

5、CAAnimationGroup 動(dòng)畫組?  


layer的基本概念 

 其實(shí)UIView之所以能顯示在屏幕上,完全是因?yàn)樗鼉?nèi)部的一個(gè)圖層,在創(chuàng)建UIView對(duì)象時(shí),UIView內(nèi)部會(huì)自動(dòng)創(chuàng)建一個(gè)圖層(CALyer對(duì)象),通過UIView的layer屬性可以訪問這個(gè)層。

基本屬性  

Bounds;position;frame;backgroundColor; opacity;cornerRadius;borderWidth; borderColor;shadowOffset; shadowColor; shadowOpacity;

我寫了一些簡(jiǎn)單的demo,大家可以看看.......??

// //? ViewController.m

?//? 簡(jiǎn)單的動(dòng)畫//?

//? Created by 李盼盼 on 16/5/16.?

//? Copyright ? 2016年 李盼盼. All rights reserved.?

//? #import "ViewController.h"??

?@interface ViewController ()?

?@property (strong, nonatomic) CALayer *subLayer;

?@property (strong, nonatomic) UIView *redView;

?@property (strong, nonatomic) CALayer *subLayer2;??

?@property (weak, nonatomic) IBOutlet UIImageView *imageView;?

?@property (assign, nonatomic) NSInteger currentIndex;

?@end?

?@implementation ViewController?

?- (void)viewDidLoad {??

? self.view.backgroundColor = [UIColor colorWithRed:234/255.0f green:234/255.0f blue:234/255.0f alpha:1];??

? [super viewDidLoad];?

//? ? 行走的方塊? ? _subLayer = [[CALayer alloc]init];? ??

?_subLayer.frame = CGRectMake(50, 50, 50, 50);?

?? _subLayer.backgroundColor = [UIColor redColor].CGColor;? ? [self.view.layer addSublayer:_subLayer];? ? ?

?//? ? 旋轉(zhuǎn)放大的方塊??

? _redView = [[UIView alloc]initWithFrame:CGRectMake(200, 100, 50, 50)];? ? _redView.backgroundColor = [UIColor yellowColor];??

? [self.view addSubview:_redView];?

? ?//? ? 慢慢放大的方塊? ?

?_subLayer2 = [[CALayer alloc]init];?

?_subLayer2.frame = CGRectMake(50, 250, 50, 50); ? ? ? ? ? _subLayer2.backgroundColor = [UIColor grayColor].CGColor; ? ? [self.view.layer addSublayer:_subLayer2];? ?

?? //? ? 仿真翻頁??

? _imageView.image = [UIImage imageNamed:@"a0.jpg"];? ?

?_currentIndex = 0;? ? ?

?}?

#pragma mark ---- 上一張?

- (IBAction)Last:(UIButton *)sender {? ?

?if (_currentIndex == 0) {? ??

? ? _currentIndex = 12;? ?

?}else{? ? ? ? _currentIndex--;? ?

?}? ? ? ?

?? _imageView.image = [UIImage imageNamed:[NSString? stringWithFormat:@"a%ld.jpg",_currentIndex]];? ?

?? //? ? 轉(zhuǎn)場(chǎng)動(dòng)畫??

? CATransition *anim = [CATransition animation];?

//? ? 過度類型?

?? anim.type = @"pageUnCurl";?

//? ? 動(dòng)畫過渡方向? ?

?anim.subtype = @"fromTop";? ?

?anim.duration = 0.8;? ??

?[_imageView.layer addAnimation:anim forKey:nil]; }

? #pragma mark ---- 下一張?

- (IBAction)next:(UIButton *)sender {

? ? if (_currentIndex == 12) {? ?

?? ? _currentIndex = 0;? ?

?}else{? ? ? ? _currentIndex++;?

?? }? ??

_imageView.image = [UIImage imageNamed:[NSString? stringWithFormat:@"a%ld.jpg",_currentIndex]];??

CATransition *anim = [CATransition animation];? ?

anim.type = @"pageCurl";?

?anim.subtype = kCATransitionFromBottom;? ??

?anim.duration = 0.8;? ??

?[_imageView.layer addAnimation:anim forKey:nil];? ??

? ? ? }

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event

{? ??

?#pragma mark ---- 旋轉(zhuǎn)放大的動(dòng)畫 ? ?

CABasicAnimation *rotationAnim = [CABasicAnimation animation];??

//? ? rotationAnim.duration = 2;??

rotationAnim.keyPath = @"transform.rotation.z";??

rotationAnim.toValue = @(3.14);??

rotationAnim.repeatCount = MAXFLOAT;?

CABasicAnimation *scaleAnim = [CABasicAnimation animation];? ? scaleAnim.duration = 2;?

scaleAnim.keyPath = @"transform";? ?

scaleAnim.toValue = [NSValuevalueWithCATransform3D:CATransform3DMakeScale(2, 2, 0)];? ? scaleAnim.repeatCount = MAXFLOAT;?

CAAnimationGroup *group = [CAAnimationGroup animation];? ? group.animations = @[rotationAnim,scaleAnim]; ?

group.duration = 5; ? ?

?group.fillMode = kCAFillModeForwards;?

?group.removedOnCompletion = NO;?

? [_redView.layer addAnimation:group forKey:nil];?

}??

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{

#pragma mark ---- 行走的方塊

CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

anim.keyPath = @"position";

anim.duration = 5.0;

NSValue *value = [NSValue valueWithCGPoint:CGPointMake(50, 50)];

NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(50, 100)];

NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];

NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];

NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];

anim.values = @[value,value1,value2,value3,value4,value5];

// 設(shè)置動(dòng)畫的執(zhí)行節(jié)奏

// kCAMediaTimingFunctionEaseInEaseOut:開始較慢,中間會(huì)加速,結(jié)束會(huì)減速

anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[self.subLayer addAnimation:anim forKey:nil];

#pragma mark ---- 慢慢放大的方塊

CABasicAnimation *anim1 = [CABasicAnimation animation];

//? 動(dòng)畫執(zhí)行是我時(shí)候修改屬性

anim1.keyPath = @"bounds";

//? ? 起始值

//? ? anim1.fromValue = [NSValue valueWithCGRect:CGRectMake(50, 250, 50, 50)];

//? ? 目標(biāo)值

anim1.toValue = [NSValue valueWithCGRect:CGRectMake(50, 250, 100, 100)];

anim1.delegate = self;

anim1.duration = 5;

[_subLayer2 addAnimation:anim1 forKey:@"animation"];

/**不刪除動(dòng)畫,同時(shí)保存動(dòng)畫最終效果**/

// 動(dòng)畫結(jié)束后自動(dòng)刪除動(dòng)畫

anim.removedOnCompletion = NO;

// 默認(rèn)保存原來的樣式:設(shè)置為使用最新的樣式

anim.fillMode = kCAFillModeForwards;

}

- (IBAction)removeAnim:(UIButton *)sender {

[_subLayer2 removeAnimationForKey:@"animation"];

}

#pragma mark ---- 動(dòng)畫的代理

-(void)animationDidStart:(CAAnimation *)anim{

NSLog(@"%s",__func__);

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

NSLog(@"%s",__func__);

}

@end

效果如下:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,696評(píng)論 6 30
  • 一、UIKit動(dòng)畫 第一種寫法是利用屬性,結(jié)合beginAnimations、commitAnimations 第...
    Gary_Tseng閱讀 1,012評(píng)論 1 3
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,326評(píng)論 1 23
  • IOS動(dòng)畫+轉(zhuǎn)場(chǎng)動(dòng)畫 #import "ViewController.h" #import "secondView...
    iOS小開發(fā)閱讀 958評(píng)論 0 1
  • 兩個(gè)人現(xiàn)在A大教學(xué)樓的頂樓看風(fēng)景。 “你有沒有對(duì)異性長時(shí)間的關(guān)注過?”他的視線依然直視著前方,似乎這句話不是對(duì)她說...
    桃子貍貍閱讀 248評(píng)論 0 0

友情鏈接更多精彩內(nèi)容