标签:
// // ViewController.m // Layer // // Created by City--Online on 15/4/9. // Copyright (c) 2015年 City--Online. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem]; btn.frame=CGRectMake(150, 250, 50, 80); [btn setTitle:@"按钮" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //图层属性 self.myview=[[UIView alloc]init]; self.myview.frame=CGRectMake(100, 100, 100, 100); //设置背景色 self.myview.layer.backgroundColor=[UIColor redColor].CGColor; //设置边框宽度 self.myview.layer.borderWidth=5; //设置圆角 self.myview.layer.cornerRadius=20; //设置透明度 self.myview.layer.opacity=0.9; //设置内容 id类型 self.myview.layer.contents=(id)[UIImage imageNamed:@"test.jpg"].CGImage; //设置边框颜色 self.myview.layer.borderColor=[UIColor greenColor].CGColor; //设置超过自图层的部分剪掉,否则设置圆角后contents中图片的四个角的部分还会显示出来 当设置此属性为yes时,阴影看不到 self.myview.layer.masksToBounds=YES; //设置阴影颜色 self.myview.layer.shadowColor=[UIColor blackColor].CGColor; // 设置阴影偏移量 width正往右 height正往下 self.myview.layer.shadowOffset=CGSizeMake(5, 15); //设置阴影透明度 默认0 self.myview.layer.shadowOpacity=1.0; [self.view addSubview:self.myview]; //图层创建 CALayer *calayer=[[CALayer alloc]init]; // calayer.frame=CGRectMake(220, 100, 100, 100); calayer.bounds=CGRectMake(0, 0, 100, 100); calayer.position=CGPointMake(260, 150); calayer.backgroundColor=[UIColor redColor].CGColor; calayer.contents=(id)[UIImage imageNamed:@"test.jpg"].CGImage; calayer.masksToBounds=YES; calayer.cornerRadius=20; calayer.borderWidth=5; [self.view.layer addSublayer:calayer]; } -(void)btnclick:(id)sender { //有时候UIView和CALayer都能解决同样的问题,UIView和CAlayer用什么比较好? //UIView继承UIResponder,可以响应用户事件,CAlayer的话是没继承UIResponder,没有响应,CAlayer是轻量的 //2d旋转变化 // self.myview.transform=CGAffineTransformMakeTranslation(100, 100);//UIView变化 // self.myview.layer.affineTransform=CGAffineTransformMakeTranslation(100, 100);//clayer变换 // // self.myview.layer.transform=CATransform3DMakeScale(2,2,0);//缩放 // self.myview.layer.transform=CATransform3DMakeTranslation(20,20,0);//平移 // self.myview.layer.transform=CATransform3DMakeRotation(3.1415926, 1, 1, 0.5);//旋转 //kvox 设置 NSValue *v=[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(100, 20, 0)]; [self.myview.layer setValue:v forKeyPath:@"transform"]; // 如果是只需要设置在某一个方向上的移动,可以参考下面的代码 // [self.myview.layer setValue:@(-100) forKeyPath:@"transform.translation.x"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:
原文地址:http://www.cnblogs.com/cuiyw/p/4410386.html