CADisplayLink的一个旋转动画

//
// ViewController.m
// qq音乐播放动画
//
// Created by 3D on 16/6/12.
// Copyright © 2016年 3D. All rights reserved.

#import "ViewController.h"
#define angleToRadian(x) (x/180.0*M_PI)
@interface ViewController ()
@property(nonatomic,strong)UIImageView     *imageView;
@property(nonatomic,strong)CADisplayLink *link;
@property(nonatomic,assign)BOOL paly;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.paly = YES;
UIButton *bution = [[UIButton alloc]init];
bution.backgroundColor = [UIColor yellowColor];
bution.frame = CGRectMake(self.view.center.x-50, 70, 100, 50);
[self.view addSubview:bution];
[bution addTarget:self action:@selector(butionClick:) forControlEvents:UIControlEventTouchUpInside];

UIImageView *imageView = [[UIImageView alloc]init];
self.imageView = imageView;
self.imageView.image = [UIImage imageNamed:@"icon180"];
imageView.layer.bounds = CGRectMake(0, 0, 240, 240);
imageView.layer.position = self.view.center;
imageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
imageView.layer.cornerRadius = 120;
imageView.layer.masksToBounds = YES;
[self.view.layer addSublayer:imageView.layer];
[self.view addSubview:imageView];

for (int i = 0; i<4; i++) {
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.contents = (id)[UIImage imageNamed:@"icon1"].CGImage;
    layer.bounds = CGRectMake(0, 0, 40, 120);
   // position 和锚点 配合固定位置        
    layer.position = CGPointMake(imageView.bounds.size.width*0.5, imageView.bounds.size.height*0.5);   
    layer.anchorPoint = CGPointMake(0.5, 1);
    layer.transform = CATransform3DMakeRotation(i*M_PI*2/4, 0, 0, 1);
    [imageView.layer addSublayer:layer];
   }
}



-(void)butionClick:(UIButton *)sender{
    NSLog(@"%d",self.paly);
   self.paly = !self.paly;
   NSLog(@"%d",self.paly);
  //link.paused控制暂停开始
   self.link.paused = self.paly;
}

-(void)rotation{
//没执行一次这个方法self.imageView.layer就会旋转angleToRadian(12)这个角度 (0,0,1) 代表(x,y,z)
self.imageView.layer.transform = CATransform3DRotate(self.imageView.layer.transform, angleToRadian(12), 0, 0, 1);
}//

- (CADisplayLink *)link {
if(_link == nil) {
    _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotation)];
//  _link.duration//每一帧之间的时间间隔。readonly。
      _link.frameInterval = 1;//默认的。将frameInterval�设置成1,是一秒默认调用60.设置成2就是一秒默认调用30次。
    [_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   }
  return _link;
}
@end

效果

CADisplayLink的一个旋转动画_第1张图片
2016-06-26 00_49_30.gif

你可能感兴趣的:(CADisplayLink的一个旋转动画)