每一个uiview都默认关联着一个CALayer,我们成这个layer为root layer
所有的非root layer都存在默认的隐私动画,隐式动画默认为1/4秒。
如果关闭默认的动画效果:可以通过动画的事务方法实现。
[CATransaction beign]
[CATransaction setDisableActions:Yes]
[CATransaction commit]
隐式动画的创建
//实例化自定义图层
CALayer *myLayer = [CALayer layer];
//设置大小
[myLayer setBounds:CGRectMake(0, 0, 100, 100)];
//设置背景颜色
[myLayer setBackgroundColor:[UIColor redColor].CGColor];
[myLayer setPosition:CGPointMake(50, 50)];
[self.view.layer addSublayer:myLayer];
在触摸事件touchesBegan事件中创建动画。完成位置变化,颜色,透明度,尺寸,圆角等操作。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
//关闭动画
// [CATransaction begin];
// [CATransaction setDisableActions:YES];
// [CATransaction commit];
//位置
[self.mylayer setPosition:location];
//颜色
NSInteger r1 = arc4random_uniform(self.colorArray.count);
[self.mylayer setBackgroundColor:[self.colorArray[r1] CGColor]];
//透明度
CGFloat alpha = (arc4random_uniform(5) + 1.0) / 10.0 + 0.5;
[self.mylayer setOpacity:alpha];
//尺寸
NSInteger size = arc4random_uniform(50) + 51;
[self.mylayer setBounds:CGRectMake(0, 0, size, size)];
//圆角
NSInteger r2 = arc4random_uniform(30);
[self.mylayer setCornerRadius:r2];
//旋转角度
CGFloat angle = arc4random_uniform(180) /180.0 * M_PI;
[self.mylayer setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
//设置content
NSInteger r3 = arc4random_uniform(self.imageArray.count);
UIImage *image = self.imageArray[r3];
[self.mylayer setContents:(id)image.CGImage];
}
CALayer还有很多的属性可以进行设置,为苹果的文档中搜索:CALayer Animatable Properties ,查看更多。
以下摘自文档
Table B-1 Layer properties and their default animations
Property
Default animation
anchorPoint
Uses the default implied CABasicAnimation object, described in Table B-2.
backgroundColor
Uses the default implied CABasicAnimation object, described in Table B-2.
backgroundFilters
Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
borderColor
Uses the default implied CABasicAnimation object, described in Table B-2.
borderWidth
Uses the default implied CABasicAnimation object, described in Table B-2.
bounds
Uses the default implied CABasicAnimation object, described in Table B-2.
compositingFilter
Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
contents
Uses the default implied CABasicAnimation object, described in Table B-2.
contentsRect
Uses the default implied CABasicAnimation object, described in Table B-2.
cornerRadius
Uses the default implied CABasicAnimation object, described in Table B-2.
doubleSided
There is no default implied animation.
filters
Uses the default implied CABasicAnimation object, described in Table B-2. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
frame
This property is not animatable. You can achieve the same results by animating the bounds and position properties.
hidden
Uses the default implied CABasicAnimation object, described in Table B-2.
mask
Uses the default implied CABasicAnimation object, described in Table B-2.
masksToBounds
Uses the default implied CABasicAnimation object, described in Table B-2.
opacity
Uses the default implied CABasicAnimation object, described in Table B-2.
position
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowColor
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowOffset
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowOpacity
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowPath
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowRadius
Uses the default implied CABasicAnimation object, described in Table B-2.
sublayers
Uses the default implied CABasicAnimation object, described in Table B-2.
sublayerTransform
Uses the default implied CABasicAnimation object, described in Table B-2.
transform
Uses the default implied CABasicAnimation object, described in Table B-2.
zPosition
Uses the default implied CABasicAnimation object, described in Table B-2.