使用核心动画 需导入QuartzCore 框架 (现在 不需要)
//#import
/*
在UIView中有一个Layer属性作为根图层 根图层没有隐式动画 根图层上可以放其他的子图层 在UIView中所有能看到的内容都包含在layer中
CALayer负责视图中显示的内容和动画
UIView负责监听和响应事件
由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性称为“隐式动画属性”。
属性 说明 是否支持隐式动画
anchorPoint 锚点、定位点 锚点的描述是相对于 *自己* x、y位置比例而言的 默认在图像中心点(0.5,0.5)的位置 决定图层的哪一个点 显示在中心点的位置 是
backgroundColor 图层背景颜色 是
borderColor 边框颜色 是
borderWidth 边框宽度 是
bounds 图层大小 是
contents 图层显示内容,例如可以将图片作为图层内容显示 是
contentsRect 图层显示内容的大小和位置 是
cornerRadius 圆角半径 是
doubleSided 图层背面是否显示,默认为YES 否
frame 图层大小和位置,不支持隐式动画,所以CALayer中很少使用frame,通常使用bounds和position代替 否
hidden 是否隐藏 是
mask 图层蒙版 是
maskToBounds 子图层是否剪切图层边界,默认为NO 是
opacity 透明度 ,类似于UIView的alpha 是
position 决定图层在父视图的位置 图层位于 *父视图* 中心点位置,类似于UIView的center 是
shadowColor 阴影颜色 是
shadowOffset 阴影偏移量 是
shadowOpacity 阴影透明度,注意默认为0,如果设置阴影必须设置此属性 是
shadowPath 阴影的形状 是
shadowRadius 阴影模糊半径 是
sublayers 子图层 是
sublayerTransform 子图层形变 是
transform 图层形变
*/
以上支持隐式动画的属性 本质是这些属性的变动默认隐含了CABasicAnimation动画
- (void)test1
{
// CALayer和UIView的关系:
// 在UIView中有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内容都包含在layer中
// CALayer负责视图中显示的内容和动画
// UIView负责监听和响应事件
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
// 图层上不可以添加监听和响应事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(testResponse:)];
[view addGestureRecognizer:tap];
}
-(void)testResponse:(UITapGestureRecognizer *)sender
{
sender.view.layer.cornerRadius = sender.view.layer.cornerRadius==0?50:0;
}
#pragma mark-----CALayer 初体验----------
-(void)LayerTest1
{
myLayer = [[CALayer alloc]init];
myLayer.backgroundColor =[UIColor redColor].CGColor;
myLayer.bounds = CGRectMake(0, 0, 100, 100);
myLayer.cornerRadius =50;
myLayer.borderWidth =2;
myLayer.borderColor = [UIColor whiteColor].CGColor;
myLayer.shadowColor =[UIColor orangeColor].CGColor;
// 设置阴影颜色 必须设置shadowOpacity阴影的透明度 (默认为0 完全透明)
myLayer.shadowOpacity = 1.0;
// 设置中心点
myLayer.position = self.view.center;
[self.view.layer addSublayer:myLayer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
myLayer.bounds =CGRectMake(0, 0, 100, 300);
myLayer.backgroundColor =[UIColor orangeColor].CGColor;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
myLayer.bounds =CGRectMake(0, 0, 100, 100);
myLayer.backgroundColor =[UIColor blackColor].CGColor;
UITouch *touch = [touches anyObject];
myLayer.position =[touch locationInView:self.view];
}