layer 的常用屬性


 

  

layer的各種屬性代碼示例:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view.
 4     
 5     // 設置圖片為圓角 (self.qweImageView.frame.size.width / 2 變成圓形)
 6     self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
 7 //    self.imageView.layer.masksToBounds = YES;
 8     // 注意:光設置上邊一句代碼是實現不了效果的(下邊的maskToBounds這個屬性影響layer層的陰影效果)
 9     // 設置layer的陰影顏色
10     self.imageView.layer.shadowColor = [UIColor blueColor].CGColor;
11     // 設置layer的透明度
12     self.imageView.layer.shadowOpacity = 0.5f;
13     // 設置陰影偏移量
14     self.imageView.layer.shadowOffset = CGSizeMake(-30, 20);
15     // 設置陰影的模糊度
16     self.imageView.layer.shadowRadius = 1;
17     
18     // 創建View
19     UIView *myView = [[UIView alloc] init];
20     myView.backgroundColor = [UIColor redColor];
21     // 設置frame
22     myView.frame = CGRectMake(100, 500, 100, 100);
23     // 設置視圖圓角 (self.qweImageView.frame.size.width / 2 如果是方形視圖變成圓形)
24     myView.layer.cornerRadius = myView.frame.size.width / 2;
25     // 設置陰影顏色
26     myView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
27     // 設置陰影偏移量
28     myView.layer.shadowOffset = CGSizeMake(10, 10);
29     // 設置陰影的透明度
30     myView.layer.shadowOpacity = 0.8f;
31     // 設置陰影的模糊度
32     myView.layer.shadowRadius = 1;
33     // 添加到View上
34     [self.view addSubview:myView];
35     
36     [self customLayer];
37 }
38 
39 - (void)customLayer {
40     // 創建一個layer對象
41     CALayer *layer = [CALayer layer];
42     // 設置對象的位置和大小
43     layer.frame = CGRectMake(300, 280, 100, 100);
44     // 設置背景顏色
45     layer.backgroundColor = [UIColor redColor].CGColor;
46     // 設置錨點
47 //    layer.anchorPoint = CGPointMake(0, 0);
48     // 設置大小
49     layer.position = CGPointMake(100, 100);
50     // layer需要添加到layer層
51     [self.view.layer addSublayer:layer];
52 }

 

 

 1 #pragma mark - CABasicAnimation
 2 - (IBAction)CABasicAnimation:(id)sender {
 3     
 4     // 第一步:創建動畫對象
 5     CABasicAnimation *basicAnimation = [CABasicAnimation animation];
 6     // 第二步:告訴layer層需要執行什么樣的動畫(后邊設置的內容為CALayer的相關屬性)
 7     basicAnimation.keyPath = @"position";  // position 改變位置的
 8     // 第三步:告訴layer從哪里來,到哪里去
 9     basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
10     basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 600)];
11     // 注意點: 如果要實現圖片不會到原來點,需要以下兩句代碼
12     basicAnimation.removedOnCompletion = NO;
13     // 設置保存動畫狀態的內容
14     basicAnimation.fillMode = kCAFillModeForwards;
15     
16     // 第四步:設置動畫持續的時長
17     basicAnimation.duration = 6.0f;
18     // 第五步:將要執行的動畫添加到calayer上
19     [self.imageView.layer addAnimation:basicAnimation forKey:@"basic"];
20     // *************翻轉效果
21     CABasicAnimation *basic = [CABasicAnimation animation];
22     basic.keyPath = @"transform";
23     // 設置翻轉的地方
24     basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, 1)];
25     basic.duration = 0.2f;
26     [self.imageView.layer addAnimation:basic forKey:@"aaaa"];
27     // 根據key去移除動畫
28     //    [self.imageView.layer removeAnimationForKey:@"basic"];
29 }
30 
31 #pragma mark - CAKeyframeAnimation
32 - (IBAction)CAKeyAnimation:(id)sender {
33     // 第一步:創建對象
34     CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animation];
35     // 第二步:設置動畫軌跡
36     keyAnimation.keyPath = @"transform.rotation";
37     // 第三步:設置翻轉的角度 (弧度計算公式:度數/180*M_PI)
38     keyAnimation.values = @[@(-300 / 180.0 * M_PI), @(300 / 180.0 * M_PI), @(-180 / 180.0 * M_PI)];
39     // 第四步:設置時長
40     keyAnimation.duration = 15.0f;
41     // 第五步:添加動畫到layer層
42     [self.view.layer addAnimation:keyAnimation forKey:@"bbbb"];
43 }
44 
45 #pragma mark - CAAnimationGroup
46 - (IBAction)CAAnimationGroup:(id)sender {
47     // 平移動畫
48     CABasicAnimation *basic1 = [CABasicAnimation animation];
49     basic1.keyPath = @"transform.translation.y";    // 按照y軸平移
50     basic1.toValue = @(400);
51     // 縮小動畫
52     CABasicAnimation *basic2 = [CABasicAnimation animation];
53     basic2.keyPath = @"transform.scale";  // 縮小
54     basic2.toValue = @(0.3);
55     // 旋轉動畫
56     CABasicAnimation *basic3 = [CABasicAnimation animation];
57     basic3.keyPath = @"transform.rotation";   // 旋轉
58     basic3.toValue = @(M_PI);
59     
60     // 需要創建管理各個動畫的動畫組
61     CAAnimationGroup *group = [CAAnimationGroup animation];
62     group.animations = @[basic1,basic2,basic3];
63     // group 會自動分配時間
64     group.duration = 5.0f;
65     
66     [self.imageView.layer addAnimation:group forKey:@"ccccc"];
67 }
68 
69 #pragma mark - CASpring
70 - (IBAction)CASpring:(id)sender {
71     // 設置對象
72     CASpringAnimation *spring = [CASpringAnimation animation];
73     spring.keyPath = @"transform.scale";
74     spring.fromValue = @1;
75     spring.toValue = @0.2;
76     spring.duration = 3.0f;
77     
78     [self.imageView.layer addAnimation:spring forKey:@"ddddd"];
79 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM