#浅析CALayer


// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m

#import "ViewController.h"

@interface ViewController ()

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UIViewLayer];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    //只有在旋转的时候才能看3D
    [UIView animateWithDuration:0.5 animations:^{
        
        // 旋转
//        self.imageV.layer.transform = CATransform3DMakeRotation(M_PI, 0.5, 0, 0);
//        self.imageV.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0);
//        self.imageV.layer.transform = CATransform3DTranslate(self.imageV.layer.transform, 100, 0, 0);
        self.imageV.layer.transform = CATransform3DRotate(self.imageV.layer.transform, M_PI, 0.5, 0, 0);
        // 把结构体转成对象
        // 通过KVC做一些快速平移旋转,缩放
//         NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
//        [self.imageV.layer setValue:@(M_PI) forKeyPath:@"transform.scale.x"];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //设置边框(边框宽度往里走的)
    self.imageV.layer.borderColor = [UIColor greenColor].CGColor;
    self.imageV.layer.borderWidth = 3;

    //设置阴影
    self.imageV.layer.shadowColor = [UIColor blueColor].CGColor;
    self.imageV.layer.shadowOpacity = 1;
    self.imageV.layer.shadowOffset = CGSizeMake(-10, 10);
    self.imageV.layer.shadowRadius = 10;

    //设置圆角
    self.imageV.layer.cornerRadius = 50;
    //超过根层以久的内容就会自动裁剪掉
    self.imageV.layer.masksToBounds = YES;

//    NSLog(@"%@",self.imageV.layer.sublayers);
//    NSLog(@"%@",self.imageV.layer.contents);
}


- (void)UIViewLayer {

    //设置边框(边框宽度往里走的)
    self.redView.layer.borderColor = [UIColor greenColor].CGColor;
    self.redView.layer.borderWidth = 3;
    //设置阴影
    self.redView.layer.shadowColor = [UIColor blueColor].CGColor;
    self.redView.layer.shadowOpacity = 1;
    self.redView.layer.shadowOffset = CGSizeMake(-10, 10);
    self.redView.layer.shadowRadius = 10;

    //设置圆角
    self.redView.layer.cornerRadius = 50;
}


@end


  • 效果图:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,827评论 25 709
  • 按预计晚了10天,由于最后几页懈怠了,没有去读,今天终于读完了。 第五本书 《怎样阅读一本书 》 第六本 《少年维...
    早睡早起的习惯阅读 184评论 0 0
  • 我费力地睁开眼睛,发现眼前一片漆黑,四周一片寂静,无声。 我的眼睛逐渐适应了光线,发现了墙上有一小抹绿光,大概是一...
    MrEricLiu阅读 264评论 0 0
  • 累 文/厉雄(西班牙) 深夜,你牵着自己的影子 回家路上,疲惫流了一地 周围全是嶙峋的目光,窥探巷子 每一个来往的...
    厉雄阅读 144评论 0 0