上篇我们看到变动position而产生的动画,这篇开始我们就讨论一下这个隐式动画,从Position属性入手:
/* The position in the superlayer that the anchor point of the layer's
* bounds rect is aligned to. Defaults to the zero point. Animatable. */
@property CGPoint position;
Animatable 非常醒目的字眼,可动画的,直接在头文件搜这个字眼可以看到31处,感谢下面参考文顶顶,如果在苹果文档里搜关键字:“ CALayer Animatable Properties” 可以看到CALayer的所有具备隐式动画属性,而且原来系统内置是通过调用CABasicAnimation 对象来生成动画效果的!!!所以说动画实质也是通过层来实现的!!!
我们来尝试设置层的hidden来观察它的动画效果:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *layer=[CALayer layer];
layer.backgroundColor=[UIColor brownColor].CGColor;
layer.bounds=CGRectMake(0, 0, 200, 200);
layer.position=CGPointMake(100, 100);
[self.view.layer addSublayer:layer];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.view.layer.sublayers[0].hidden)
{
self.view.layer.sublayers[0].hidden = NO;
}
else
{
self.view.layer.sublayers[0].hidden = YES;
}
}
我们能看到一个短暂的动画,这个默认的时间是0.25秒,并且通过测试和查看文档,我们知道rootLayer是不存在隐式动画的功能的,因为这个根图层更多的作用是一种“容器的管理层”
我们也可以通过动画事务类来关闭动画效果:
[CATransaction begin];
[CATransaction setDisableActions:YES];
//Do Animatable Transaction.
[CATransaction commit];
当然了你也可以同时去修改多个属性,这样系统会自动绘制出炫酷的动画效果
将上篇文章的touches修改一下:
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = [UIScreen mainScreen].bounds.size;
CALayer* layer = [[CALayer alloc] init];
layer.backgroundColor = [UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1.0].CGColor;
layer.position = CGPointMake(size.width/2, size.height/2);
layer.bounds = CGRectMake(0, 0, kWIDTH,kWIDTH);
layer.cornerRadius = kWIDTH/2;
layer.shadowColor=[UIColor grayColor].CGColor;
layer.shadowOffset=CGSizeMake(2, 2);
layer.shadowOpacity=.9;
[self.view.layer addSublayer:layer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
[self.view.layer.sublayers[0] setPosition:location];
//背景
NSInteger rr = arc4random() % 10 ;
NSInteger gg = arc4random() % 10 ;
NSInteger bb = arc4random() % 10 ;
[self.view.layer.sublayers[0] setBackgroundColor:[UIColor colorWithRed:rr green:gg blue:bb alpha:1].CGColor];
//透明度
CGFloat alpha = (arc4random_uniform(5) + 1.0) / 10.0 + 0.5;
[self.view.layer.sublayers[0] setOpacity:alpha];
//圆角
NSInteger rd = arc4random_uniform(30);
[self.view.layer.sublayers[0] setCornerRadius:rd];
//旋转角度
CGFloat angle = arc4random_uniform(180) / 180.0 * M_PI;
[self.view.layer.sublayers[0] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
}
你会发现变得更有趣了!!!
当然你还设置别的还没有修改到的内容来产生效果,例如contents:
//设置content
NSInteger r3 = arc4random_uniform(self.imageArray.count); //随机生成.
UIImage *image = self.imageArray[r3];
[self.mylayer setContents:(id)image.CGImage];
参考:
http://www.cnblogs.com/wendingding/p/3800736.html
http://www.jianshu.com/p/930cea99023d