CALayer
Posted 恒远也
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CALayer相关的知识,希望对你有一定的参考价值。
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 5 @property (weak, nonatomic) IBOutlet UIView *uiview; 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 #pragma mark - layer层的一些常用属性 15 //将视图设置为圆角 16 self.imageView.layer.cornerRadius = 10; 17 //注意:只设置上边一个属性是实现不了圆角效果的,还需要设置如下属性(这个属性也影响阴影的效果) 18 // self.imageView.layer.masksToBounds = YES; 19 //设置阴影 20 self.imageView.layer.shadowOffset = CGSizeMake(-20, 10); 21 //设置颜色 22 self.imageView.layer.shadowColor = [UIColor redColor].CGColor; 23 //设置透明度 24 self.imageView.layer.shadowOpacity = 1.0; 25 //阴影的模糊度 26 self.imageView.layer.shadowRadius = 10; 27 28 29 // self.uiview.layer.cornerRadius = 10; 30 // // self.uiview.layer.masksToBounds = YES; 31 // 32 // self.uiview.layer.shadowOffset = CGSizeMake(10, 10); 33 // 34 // self.uiview.layer.shadowColor = [UIColor magentaColor].CGColor; 35 // 36 // self.uiview.layer.shadowOpacity = 1.0; 37 38 39 40 #pragma mark - 自定义layer 41 // [self customLayer]; 42 } 43 #pragma mark - CABasicAnimation按钮的响应方法 44 - (IBAction)CABasicAnimation:(UIButton *)sender { 45 //1.创建动画对象 46 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; 47 //2.告诉系统需要执行什么样的动画,字符串不能随便给,必须是CALayer类中的一个属性 48 basicAnimation.keyPath = @"position"; 49 //3.告诉动画从哪里开始到哪里 50 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 51 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; 52 #warning 如果不想让控件回到原来的位置,需要设置一下两个属性 53 basicAnimation.removedOnCompletion = NO; 54 //保存动画最新转态的属性 55 basicAnimation.fillMode = kCAFillModeForwards; 56 57 //4.动画的延续时间 58 basicAnimation.duration = 3.0; 59 //5.对控件的layer层添加动画 60 [self.imageView.layer addAnimation:basicAnimation forKey:@"basic"]; 61 62 /******************实现翻转效果*****************/ 63 CABasicAnimation *basic = [CABasicAnimation animation]; 64 basic.keyPath = @"transform"; 65 basic.duration = 3.0; 66 //翻转的一个角度设置,第一个是角度,后边三个是以哪个轴旋转 67 basic.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_2_PI, 1, 0, 0)]; 68 [self.imageView.layer addAnimation:basic forKey:@"basic1"]; 69 70 //移除不需要的动画,根据上面给的关键字移除 71 [self.imageView.layer removeAnimationForKey:@"basic"]; 72 } 73 #pragma mark - 帧动画 74 - (IBAction)CAkeyframeAnimation:(UIButton *)sender { 75 //1.需要动画的对象 76 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation]; 77 //2.设置动画轨迹,transform点后边的内容根据 78 keyFrameAnimation.keyPath = @"transform.rotation"; 79 //3.设置要翻转的角度[计算弧度的公式:度数/180 * M_PI],下面的方法相当于上面的fromValue,toValue 80 keyFrameAnimation.values = @[@(-45/180.0*M_PI),@(45/180.0*M_PI)]; 81 keyFrameAnimation.repeatCount = 1; 82 //4.设置动画时长 83 keyFrameAnimation.duration = 3; 84 //5.添加到layer层上 85 [self.imageView.layer addAnimation:keyFrameAnimation forKey:@"keyFrame"]; 86 87 } 88 89 -(void)customLayer{ 90 //创建layer的一个对象 91 CALayer *myLayer = [CALayer layer]; 92 //设置相关的位置和大小 93 myLayer.bounds = CGRectMake(0, 0, 100, 100); 94 //设置背景颜色 95 myLayer.backgroundColor = [UIColor redColor].CGColor; 96 //设置一个锚点 97 myLayer.anchorPoint = CGPointMake(0, 0); 98 //设置锚点的位置 99 myLayer.position = CGPointMake(100, 100); 100 //添加到父视图的layer 101 [self.view.layer addSublayer:myLayer]; 102 103 } 104 - (IBAction)animationGroup:(UIButton *)sender { 105 //平移动画 106 CABasicAnimation *basc1 = [CABasicAnimation animation]; 107 basc1.keyPath = @"transform.translation.y"; 108 basc1.toValue = @(400); 109 110 //翻转动画(缩放) 111 CABasicAnimation *basc2 = [CABasicAnimation animation]; 112 basc2.keyPath = @"transform.scale"; 113 basc2.toValue = @(0.2); 114 115 //旋转动画 116 CABasicAnimation *basic3 = [CABasicAnimation animation]; 117 basic3.keyPath = @"transform.rotation"; 118 basic3.toValue = @(M_PI_2); 119 120 //需要创建存放以上三种动画的动画组 121 CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 122 123 animationGroup.animations = @[basc1,basc2,basic3]; 124 125 animationGroup.duration = 3.0; 126 127 [self.imageView.layer addAnimation:animationGroup forKey:@"animationGroup"]; 128 } 129 #pragma mark - CATransition动画 130 - (IBAction)translationAnimation:(UIButton *)sender { 131 //创建动画对象 132 CATransition *transition = [CATransition animation]; 133 transition.type = @"cube"; 134 transition.subtype = kCATransitionFade; 135 transition.duration = 1; 136 [self.imageView.layer addAnimation:transition forKey:@"transition"]; 137 } 138 #pragma spring 139 140 - (IBAction)spring:(UIButton *)sender { 141 //创建动画对象 142 CASpringAnimation *spring = [CASpringAnimation animation]; 143 spring.keyPath = @"transform.scale"; 144 spring.fromValue = @1; 145 spring.toValue = @0.75; 146 spring.duration = 3; 147 [self.imageView.layer addAnimation:spring forKey:@"spring"]; 148 149 } 150 151 - (void)didReceiveMemoryWarning { 152 [super didReceiveMemoryWarning]; 153 // Dispose of any resources that can be recreated. 154 } 155 156 @end
以上是关于CALayer的主要内容,如果未能解决你的问题,请参考以下文章