CALayer 簡單用法

首先了解一下CALayer的基本經常使用的屬性:ide

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4     
 5     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
 6     
 7     button.frame = CGRectMake(100, 50, 175, 175);
 8     
 9     [button setBackgroundImage:[UIImage imageNamed:@"06.jpg"] forState:UIControlStateNormal];
10     
11     [self.view addSubview:button];
12     
13     button.layer.cornerRadius = 20 ;
14     
15     button.layer.borderColor = [[UIColor yellowColor]CGColor];
16     
17     button.layer.borderWidth = 2 ;
18     
19 //    button.clipsToBounds = YES ;
20 
21     //陰影顏色
22     button.layer.shadowColor = [[UIColor blackColor]CGColor];
23     
24     //陰影的偏移距離
25     button.layer.shadowOffset = CGSizeMake(20, 20);
26     
27     //陰影的透明度
28     button.layer.shadowOpacity = 0.8 ;
29     
30     //陰影
31     button.layer.shadowRadius = 20 ;
32     
33     //是否裁剪邊框以外的
34 //    button.layer.masksToBounds = YES ;
35     
36     
37     //CALayer負責視圖的渲染 UI中真正負責繪圖的部分 沒有用戶交互,僅僅是展現視圖內容,通常用來作動畫
38     CALayer *layer = [CALayer layer];
39     
40     layer.frame = CGRectMake(100, 300, 175, 175);
41     
42     layer.backgroundColor = [[UIColor yellowColor]CGColor];
43     
44     layer.contents = (id)[[UIImage imageNamed:@"06.jpg"]CGImage];
45     
46     [self.view.layer addSublayer:layer];
47     
48     layer.shadowOffset = CGSizeMake(20, 20);
49     
50     layer.shadowColor = [[UIColor redColor]CGColor];
51     
52     layer.shadowOpacity = 0.3 ;
53  
54     
55 }

以上就是CALayer經常使用的屬性,接下來咱們用CALayer寫個動畫的小Demo動畫

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 
  5 @property (nonatomic,retain) CALayer *layer ;
  6 
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 -(void)dealloc{
 12     
 13     [_layer release];
 14     [super dealloc];
 15 }
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19     // Do any additional setup after loading the view, typically from a nib.
 20     
 21     self.layer = [CALayer layer];
 22     
 23     self.layer.bounds = CGRectMake(0, 0, 100, 100);
 24 
 25     //設置錨點
 26     self.layer.position = CGPointMake(187.5, 200);
 27     
 28     //設置錨點在layer上的比例
 29     self.layer.anchorPoint = CGPointMake(0.2, 0.2) ;
 30     
 31     self.layer.cornerRadius = 20 ;
 32     
 33     self.layer.borderWidth = 2 ;
 34     
 35     self.layer.shadowOffset = CGSizeMake(20, 20);
 36     
 37     self.layer.shadowOpacity = 0.2 ;
 38     
 39     self.layer.doubleSided = YES ;
 40     
 41     self.layer.borderColor = [[UIColor redColor]CGColor];
 42     
 43     self.layer.backgroundColor = [[UIColor brownColor]CGColor];
 44     
 45     [self.view.layer addSublayer:self.layer];
 46     
 47     [_layer release];
 48     
 49 }
 50 
 51 - (void)didReceiveMemoryWarning {
 52     [super didReceiveMemoryWarning];
 53     // Dispose of any resources that can be recreated.
 54 }
 55 
 56 
 57 #pragma mark -- 平移CALayer
 58 - (IBAction)buttonAction1:(UIButton *)sender {
 59 
 60     //建立一個基本動畫對象
 61     CABasicAnimation *animation = [CABasicAnimation animation];
 62     
 63     //動畫的類型
 64     //keyPath layer的某個屬性
 65     animation.keyPath = @"position";
 66     
 67     //動畫的持續時間
 68     animation.duration = 2 ;
 69     
 70     //toValue 就是一個臨時值,經過CALayer作的動畫不會改變layer屬性自己的值
 71     animation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 400)];
 72     
 73     //若是要保留動畫的最終目的,下面兩個屬性分都須要設置
 74     
 75     //保持動畫的最新狀態
 76     animation.fillMode = kCAFillModeBackwards ;
 77     
 78     //設置動畫完成後以後不刪除
 79     animation.removedOnCompletion = NO ;
 80     
 81     animation.delegate = self ;
 82     
 83     [self.layer addAnimation:animation forKey:nil];
 84     
 85     
 86     
 87 }
 88 
 89 -(void)animationDidStart:(CAAnimation *)anim
 90 {
 91     
 92     NSLog(@"%@",NSStringFromCGRect(self.layer.bounds));
 93     
 94 }
 95 
 96 
 97 #pragma mark --實現縮放
 98 - (IBAction)buttonAction2:(UIButton *)sender {
 99     
100     //1.建立一個基本動畫
101     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
102     
103     //2.設置動畫持續時間
104     animation.duration = 2 ;
105     
106     //3.設置動畫的最終效果
107     animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
108     
109     //4.使動畫保留最終狀態
110     animation.fillMode = kCAFillModeForwards ;
111     
112     animation.removedOnCompletion = NO ;
113     
114     //5.將動畫對象付給layer
115     [self.layer addAnimation:animation forKey:nil];
116     
117 }
118 
119 - (IBAction)buttonAction3:(UIButton *)sender {
120     
121     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
122     
123     animation.duration = 2 ;
124     
125     animation.fillMode = kCAFillModeForwards ;
126     
127     animation.removedOnCompletion = NO ;
128     
129     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI / 3, 0, 0, 1)];
130     
131     [self.layer addAnimation:animation forKey:nil];
132     
133     
134 }
相關文章
相關標籤/搜索