Layer基本使用

Layer基本使用_第1张图片
layer基本使用.gif

1、知识点梳理

  • CALayer的基本属性的使用
    • 图片设置为圆角,边框与阴影等效果
  • 旋转动画
    • 利用layer的transform形变属性 -> 旋转动画
       // 方式一:利用layer的transform形变属性,来做旋转动画
       _imageV.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
  • 利用KVC -> 旋转动画
       // KVC去做动画
    [_imageV.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 1, 0)] forKeyPath:@"transform"];

2、具体实现代码

 #import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // imageLayer
    // cornerRadius修改的是根层的圆角半径
    _imageV.layer.cornerRadius = 50;
    
    // 超出根层部分的全部裁剪掉
    _imageV.layer.masksToBounds = YES;
    
    // 设置边框
    _imageV.layer.borderWidth = 1;
    _imageV.layer.borderColor = [UIColor whiteColor].CGColor;
    
    // 什么时候使用裁剪图片 1.控件不是正方形 2.做动画
    
    NSLog(@"%@",_imageV.layer.contents);
    
    [self viewLayer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:1 animations:^{
        
        // 方式一:利用layer的transform形变属性,来做旋转动画
        _imageV.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
        
        // KVC去做动画
//        [_imageV.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 1, 0)] forKeyPath:@"transform"];

    } completion:nil];
}

- (void)viewLayer
{
    // 设置阴影
    _redView.layer.shadowOpacity = YES;
    _redView.layer.shadowColor = [UIColor yellowColor].CGColor;
//        _redView.layer.shadowOffset = CGSizeMake(10, 10);
    _redView.layer.shadowRadius = 20;
    
    // 设置圆角半径
    _redView.layer.cornerRadius = _redView.bounds.size.width  * 0.5;
    // 设置边框
    _redView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    _redView.layer.borderWidth = 1;
}
@end

你可能感兴趣的:(Layer基本使用)