源码核心动画01-CALayer-基本使用(了解)

 

//  ViewController.m
//  01-CALayer-基本使用(了解)
#import "ViewController.h"

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

@end

@implementation ViewController

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 图层形变
    
    // 缩放
    [UIView animateWithDuration:1 animations:^{
        
//        _redView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
//        _redView.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1);
        
        // 快速进行图层缩放,KVC
        // x,y同时缩放0.5
//        [_redView.layer setValue:@0.5 forKeyPath:@"transform.scale"];
        
        [_redView.layer setValue:@(M_PI) forKeyPath:@"transform.rotation"];
        
        
    }];
    
    
}

- (void)imageLayer
{
    // cornerRadiu设置控件的主层边框
    _imageView.layer.cornerRadius = 50;
    
    NSLog(@"%@",_imageView.layer.contents)
    ;
    // 超出主层边框的内容全部裁剪掉
    _imageView.layer.masksToBounds = YES;
    
    // 设置边框
    _imageView.layer.borderColor = [UIColor whiteColor].CGColor;
    _imageView.layer.borderWidth = 1;
    
    // 如何判断以后是否需要裁剪图片,就判断下需要显示图层的控件是否是正方形。
}

- (void)viewLayer
{
    // 设置阴影
    // Opacity:不透明度
    _redView.layer.shadowOpacity = 1;
    //    _redView.layer.shadowOffset = CGSizeMake(10, 10);
    // 注意:图层的颜色都是核心绘图框架,通常。CGColor
    _redView.layer.shadowColor = [UIColor yellowColor].CGColor;
    _redView.layer.shadowRadius = 10;
    
    
    // 圆角半径
    _redView.layer.cornerRadius = 50;
    
    // 边框
    _redView.layer.borderWidth = 1;
    _redView.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

04-CALayer新建图层(了解) 

//  ViewController.m
//  04-CALayer-新建图层(了解)
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 创建图层
    CALayer *layer = [CALayer layer];
    
    layer.frame = CGRectMake(50, 50, 200, 200);
    
    layer.backgroundColor = [UIColor redColor].CGColor;
    
    // 设置图层内容
    layer.contents = (id)[UIImage imageNamed:@"阿狸头像"].CGImage;
    
    
    [self.view.layer addSublayer:layer];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 04-CALayer隐式动画

//  ViewController.m
//  05-CALayer-隐式动画(了解)
#import "ViewController.h"
#define angle2radion(angle) ((angle) / 180.0 * M_PI)
@interface ViewController ()
@property (nonatomic, weak) CALayer *layer;
@property (weak, nonatomic) IBOutlet UIView *blueView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CALayer *layer = [CALayer layer];
    
    layer.position = CGPointMake(200, 150);
    
//    layer.anchorPoint = CGPointZero;
    
    layer.bounds = CGRectMake(0, 0, 80, 80);
    
    layer.backgroundColor = [UIColor redColor].CGColor;
    
    [self.view.layer addSublayer:layer];
    
    _layer = layer;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 旋转
        // 1 ~ 360
        
    _blueView.layer.transform = CATransform3DMakeRotation(angle2radion(arc4random_uniform(360) + 1), 0, 0, 1);
    
    _blueView.layer.position = CGPointMake(arc4random_uniform(200) + 20, arc4random_uniform(400) + 50);
    
    _blueView.layer.cornerRadius = arc4random_uniform(50);
    
    _blueView.layer.backgroundColor = [self randomColor].CGColor;
    
    _blueView.layer.borderWidth = arc4random_uniform(10);
    _blueView.layer.borderColor = [self randomColor].CGColor;
    
}

- (UIColor *)randomColor
{
    
    CGFloat r = arc4random_uniform(256) / 255.0;
    CGFloat b = arc4random_uniform(256) / 255.0;
    CGFloat g = arc4random_uniform(256) / 255.0;
    

    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
原文地址:https://www.cnblogs.com/laugh/p/6708325.html