核心动画实践
_layer = [CALayerlayer];
[self.view.layeraddSublayer:_layer];
_layer.frame =CGRectMake(110,200, 80, 80);
_layer.backgroundColor = [[UIColorredColor]CGColor];
_layer.borderColor = [[UIColorgreenColor]CGColor];
_layer.borderWidth =3;
_layer.shadowColor = [[UIColoryellowColor]CGColor];//设置阴影
_layer.shadowOffset =CGSizeZero;//阴影偏移
_layer.shadowRadius =15;//阴影半径
_layer.cornerRadius =10;
_layer.anchorPoint =CGPointZero;//设置锚点
_layer.shadowOpacity =0.9;//设置阴影不透明度
//文字涂层
CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = CGRectMake(0, 200, self.view.bounds.size.width, 200);
textLayer.backgroundColor = [[UIColor purpleColor]CGColor];
textLayer.string = @"Swift 编程指南Swift 编程指南Swift编程指南Swift 编程指南Swift 编程指南Swift编程指南";
textLayer.foregroundColor = [[UIColor cyanColor]CGColor];
textLayer.alignmentMode = kCAAlignmentCenter;
textLayer.fontSize = 20;
//设置换行
textLayer.wrapped = YES;
textLayer.font = CGFontCreateWithFontName((CFStringRef)@"Zapfino");
[self.view.layer addSublayer:textLayer];
//渐变涂层
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
[self.view.layer addSublayer:gradientLayer];
gradientLayer.frame = self.view.layer.bounds;
gradientLayer.backgroundColor = [[UIColor redColor]CGColor];
gradientLayer.opacity = 0.5f;
gradientLayer.colors = @[(id)[UIColor orangeColor].CGColor,
(id)[UIColor yellowColor].CGColor,
(id)[UIColor blueColor].CGColor
];
gradientLayer.startPoint = CGPointMake(0.5, 0.5);
gradientLayer.endPoint = CGPointMake(1, 1);
//设置渐变色的区域位置
gradientLayer.locations = @[@0, @.6, @1];
[self.view.layer insertSublayer:gradientLayer below:layer1];
//基本核心动画
- (void)doBasicAnimation{
1.设置边框变色的动画
CABasicAnimation *borderAnimation = [CABasicAnimationanimation];
//kvc方式,制定动画要操作的属性
borderAnimation.duration = 0.5;
//自动逆转
borderAnimation.autoreverses = NO;
//重复次数
borderAnimation.repeatCount = 5;
[borderAnimation setKeyPath:@"borderColor"];
borderAnimation.toValue = (id)[[UIColorredColor]CGColor];
//可以根据key来管理移除已经添加的动画
// [_layer addAnimation:borderAnimation forKey:@"layer_border_color_animation"];
2.设置沿z轴旋转的动画
CABasicAnimation *zRotation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];
zRotation.duration = 3.0f;
// zRotation.fromValue = @(0);
// zRotation.toValue = @(M_PI);
zRotation.byValue = @(M_PI/4);
//把动画从layer上移除
// zRotation.removedOnCompletion = NO;
zRotation.autoreverses = YES;
// zRotation.fillMode = kCAFillModeBoth;
// [_layer addAnimation:zRotation forKey:@"layer_rotation_z_animation"];
//缩放动画
3.设置沿y轴缩放动画
CABasicAnimation *scaleAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale.y"];
scaleAnimation.duration = 3.0f;
scaleAnimation.autoreverses = YES;
// scaleAnimation.removedOnCompletion = NO;
//动画填充模式
// scaleAnimation.fillMode = kCAFillModeBoth;
scaleAnimation.toValue = @(0.5);
// [_layer addAnimation:scaleAnimation forKey:@"layer_scale_animation"];
//动画组,可以把一些动画组合,起来,同时添加进行控制,
CAAnimationGroup *animationGroup = [CAAnimationGroupanimation];
animationGroup.animations = @[borderAnimation, zRotation, scaleAnimation];
animationGroup.duration = 3;
[_layer addAnimation:animationGroupforKey:@"animations_group"];
//关键帧动画
- (void)doKeyFrameAnimation{
//根据关键点来移动layer
CAKeyframeAnimation *moveKFAni = [CAKeyframeAnimationanimation];
moveKFAni.keyPath = @"position";
moveKFAni.duration = 3.0f;
CGPoint pos = _layer.position;
moveKFAni.values = @[[NSValue valueWithCGPoint:pos],
[NSValue valueWithCGPoint:CGPointMake(0,400)],
[NSValue valueWithCGPoint:CGPointMake(320,400)],
[NSValue valueWithCGPoint:pos]];
//走的进度,百分数,完全走完是1
moveKFAni.keyTimes = @[@0.0f, @0.8f, @0.9f, @1.0f];
[_layeraddAnimation:moveKFAniforKey:@"key_frame_move_animation"];
}
//过渡动画,动画和UIViewController的push的效果一样
- (void)doTransitionAnimation{
CATransition *tranAni = [CATransitionanimation];
tranAni.duration = 1.0f;
// `fade', `moveIn', `push' and `reveal'. Defaults to `fade'.
tranAni.type =kCATransitionFade;
tranAni.subtype =kCATransition;
tranAni.type = @"suckEffect";
[_layeraddAnimation:tranAniforKey:@"transition_animation"];
}
//用核心动画中的事务来对layer进行操作隐式
- (void)doTransaction{
// CATransaction
//直接赋值就可以隐式调用事物动画
// _layer.shadowOpacity = 0.1f;
//显示调用
//一般写代码都把长队的begin和 commit都写出来
[CATransactionbegin];
[CATransactionsetAnimationDuration:1];
// self.view.layer.backgroundColor = [[UIColor redColor]CGColor];
_layer.backgroundColor = [[UIColorblueColor]CGColor];
[CATransactionsetCompletionBlock:^{
//递归
if (_isTransactionRunning) {
[self doTransaction];
}
}];
if (_layer.shadowOpacity >=0.9f) {
_layer.shadowOpacity =0.1;
}else{
_layer.shadowOpacity =0.9;
}
[CATransactioncommit];
//写在外面隐式调用
demo:
https://github.com/lunarboat/CAAnimationTest.git