CALayer 花瓣飄落 轉場動畫-CATransation 動畫組 UIView動畫封裝

#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define LayerWidth 50
@interface ViewController (){
    CALayer *layer;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    layer = [[CALayer alloc]init];
    //設置寬高
    layer.bounds = CGRectMake(0,0, LayerWidth, LayerWidth);
    //設置中心點位置
    layer.position = CGPointMake(WIDTH/2.0, HEIGHT/2.0);
    layer.backgroundColor = [UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1].CGColor;
    [self.view.layer addSublayer:layer];
    //設置圓角
    layer.cornerRadius = LayerWidth/2;
    //設置陰影
    layer.shadowColor = [UIColor grayColor].CGColor;
    layer.shadowOffset = CGSizeMake(2, 2);
    layer.shadowOpacity = 0.9;//透明度(0-1)
    //錨點(x,y 範圍都是0-1)
    layer.anchorPoint = CGPointMake(1, 1);
    NSLog(@"%@",layer);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //獲取點擊位置
    UITouch *touch = [touches anyObject];
    NSLog(@"點擊的位置是:%@",NSStringFromCGPoint([touch locationInView:self.view]));
    //獲取Layer
    NSLog(@"%@",self.view.layer.sublayers);
    layer.position =[touch locationInView:self.view];
    //放大
    CGFloat width = layer.bounds.size.width;
    if(width == LayerWidth){
        width = LayerWidth *4;
    }
    else{
        width = LayerWidth;
        
    }
    layer.bounds = CGRectMake(0, 0, width, width);
    layer.cornerRadius =width/2;//圓角是更具當前圓形寬度來設置
}
@end

數組



//花瓣飄落oop

//
//  ViewController.m
//  花瓣飄落
//
//  Created by DC020 on 15/12/22.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    CALayer *_layer;//建立一個圖層
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //設置背景
    UIImage *backgroundImage = [UIImage imageNamed:@"background"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
    //自定義一個圖層(花瓣)
    _layer = [[CALayer alloc]init];
    _layer.bounds = CGRectMake(0, 0, 16, 30);
    _layer.position = CGPointMake(50, 200);
    _layer.contents = (id)[UIImage imageNamed:@"petal"].CGImage;
    [self.view.layer addSublayer:_layer];
    //執行動畫
    [self keyFrameAnimation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 關鍵幀動畫
-(void)keyFrameAnimation{
    //1.建立關鍵幀動畫
    CAKeyframeAnimation *keyframeAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //2.設置關鍵幀
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, _layer.position.x, _layer.position.y);//路徑移動到起點
    CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, 50, 400);//繪製兩次貝塞爾曲線
    
    keyframeAni.path = path;//設置動畫沿着路徑進行
    
    
    
    
    
//    NSValue *key1 = [NSValue valueWithCGPoint:_layer.position];
//    //對於關鍵幀動畫,初始值不能省略
//    NSValue *key2 = [NSValue valueWithCGPoint:CGPointMake(80, 250)];
//    NSValue *key3 = [NSValue valueWithCGPoint:CGPointMake(20, 320)];
//    NSValue *key4 = [NSValue valueWithCGPoint:CGPointMake(100, 410)];
//    keyframeAni.values = @[key1,key2,key3,key4];
    //設置一些其餘的屬性
    keyframeAni.duration = 3.0;
    keyframeAni.beginTime = CACurrentMediaTime() + 2;//設置延遲兩秒鐘執行
    keyframeAni.autoreverses = YES;//從終點返回起點
    keyframeAni.repeatCount = HUGE_VALF;
    //添加到動畫圖層
    [_layer addAnimation:keyframeAni forKey:@"keyFrameAnimation"];
}




//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//    CGPoint location = [touch locationInView:self.view];
//    //1.建立動畫並制定動畫屬性
//    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
//    //2.設置動畫屬性初始值和結束值
//    //初始值能夠不設置,默認圖層初始位置
//    basicAnimation.toValue = [NSValue valueWithCGPoint:location];
//    
//    //3.設置其餘動畫屬性
//    basicAnimation.duration = 1.0;//動畫時間
//    basicAnimation.repeatCount = 1;//循環次數
//    basicAnimation.delegate = self;
//    //記錄鼠標點擊位置,在動畫結束後使用
//    [basicAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"animationLocation"];
//    
//    
//    //4.添加動畫.注意key至關於給動畫命名,之後能夠使用此名稱獲取該動畫
//    [_layer addAnimation:basicAnimation forKey:@"basicAnimation"];
//}
//#pragma mark 動畫代理方法
//#pragma mark 動畫結束的時候會觸發
//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//    //開啓動畫事務
//    [CATransaction begin];
//    //禁用隱式動畫
//    [CATransaction setDisableActions:YES];
//    _layer.position = [[anim valueForKey:@"animationLocation"] CGPointValue];
//    //遞交動畫事務
//    [CATransaction commit];
//}
@end

動畫



//轉場動畫-CATransationurl

//
//  ViewController.m
//  轉場動畫-CATransation
//
//  Created by DC020 on 15/12/22.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
#define IMAGE_COUNT 9//圖片數量

@interface ViewController (){
    UIImageView *_imageView;
    int _current;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _current = 0;
    //定義一個圖片控件
    _imageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];
    [self.view addSubview:_imageView];
    //添加手勢
    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;//設置手勢方向
    [self.view addGestureRecognizer:leftSwipeGesture];
    
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGesture];
    
}
-(void)leftSwipe:(UISwipeGestureRecognizer *)gesture{
    [self transitionAnimation:YES];//YES表示下一張圖片
}
-(void)rightSwipe:(UISwipeGestureRecognizer *)gesture{
    [self transitionAnimation:NO];//NO表示上一張圖片
}
#pragma mark 轉場動畫
-(void)transitionAnimation:(BOOL)isNext{
    //1.建立動畫對象
    CATransition *transition = [[CATransition alloc]init];
    //2.設置對話類型,對於蘋果官方沒有公開的動畫類型只能使用字符串
    //cube   立方體翻轉效果
    //oglFlip  翻轉效果
    //suckEffect  收縮效果
    //rippleEffect  水滴波紋效果
    //pageCurl  向上翻頁效果
    //pageUnCurl  向下翻頁效果
    //cameraIrisHollowOpen  攝像頭打開效果
    //cameraIrisHollowClose  攝像頭關閉效果
    transition.type = @"cameraIrisHollowOpen";
    //設置子類型
    if (isNext) {
        transition.subtype = kCATransitionFromRight;
    }
    else{
        transition.subtype = kCATransitionFromLeft;
    }
    //設置動畫時間
    transition.duration = 1.0;
    //設置轉場後的新視圖
    if(isNext){
        _current = (_current + 1) %IMAGE_COUNT;
    }
    else{
        _current = (_current - 1 + IMAGE_COUNT) %IMAGE_COUNT;
    }
    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];
    //添加動畫
    [_imageView.layer addAnimation:transition forKey:@"TC"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

.net


//動畫組代理

//
//  ViewController.m
//  動畫組
//
//  Created by DC020 on 15/12/22.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化一個圖層
    CALayer *_layer = [[CALayer alloc]init];
    _layer.frame = CGRectMake(20, 20, 40, 40);
    _layer.backgroundColor = [UIColor greenColor].CGColor;
    [self.view.layer addSublayer:_layer];
    //移動位置的動畫:keyPath ==> position;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(315, 607)];
    //對layer放大
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:3.0];
    
    //以z軸進行旋轉
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0];
    rotateAnimation.toValue = [NSNumber numberWithFloat:6*M_PI];//旋轉3周
    //把上面的動畫組合起來
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];//動畫組初始化
    groupAnimation.autoreverses = YES;
    groupAnimation.duration = 3.0;
    groupAnimation.repeatCount = HUGE_VALF;
    groupAnimation.animations = @[animation,scaleAnimation,rotateAnimation];
    [_layer addAnimation:groupAnimation forKey:@"layerMove"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

orm


//逐幀動畫對象

//
//  ViewController.m
//  逐幀動畫
//
//  Created by DC020 on 15/12/22.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    NSMutableArray *_muArray;//存放動畫的全部
    UIImageView *_imageView;
    UIImage *_image;
    
    
    CALayer *_layer;
    int current;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    current = 0;
    _muArray = [NSMutableArray array];
//    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 375)];
//    [self.view addSubview:_imageView];
    //建立圖層
    _layer = [[CALayer alloc]init];
    _layer.frame = CGRectMake(0, 20, 298, 275);
    [self.view.layer addSublayer:_layer];
    
    
    
    //加載全部圖片
    for(int i = 1;i <= 87;i++){
        _image = [UIImage imageNamed:[NSString stringWithFormat:@"dazhao_%d",i]];
        [_muArray addObject:_image];
    }
    
    
    
    
    //定義時鐘對象
    CADisplayLink *displayLink =[CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
    //添加時鐘對象到主運行循環
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    
//    //設置動畫
//    _imageView.animationImages = _muArray;//動畫數組(包括全部動畫圖片)
//    _imageView.animationDuration = 0.01*[_muArray count];//1秒顯示10張
//    _imageView.animationRepeatCount = HUGE_VALF;//重複無限次
//    [_imageView startAnimating];//開始動畫
}
static int s = 0;
-(void)step{
    s++;
    if (s %1 == 0) {
        UIImage *image = _muArray[current];
        _layer.contents = (id)image.CGImage;//更新圖片
        current = (current +1)%87;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


圖片


////
//  ViewController.m
//  UIView動畫封裝-彈簧
//
//  Created by DC020 on 15/12/22.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    UIImageView *_imageView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //建立圖像
    _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball"]];
    _imageView.center = CGPointMake(375/2.0, 667/2.0);
    [self.view addSubview:_imageView];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    //建立彈性動畫
    //阻尼:範圍0-1,越接近0,彈性效果越明顯
    //velocity:彈性復原速度
    [UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:100 options:UIViewAnimationOptionCurveLinear animations:^{
        _imageView.center = location;//最終小球位置
    } completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

事務

相關文章
相關標籤/搜索