CALayer动画比UIView动画要难点,但是功能也强大。
每个View都一个layer层
使用Layer层可以设置View的圆角,阴影.
使用Layer时,要注意锚点的使用,和position的区别。
CALayer分三中
basic,keyFrame,CATransition这三种,group是组合,property是抽象类,不能直接使用。
baise中的keyPath使用时要注意。
#import "ViewController.h"
@interface ViewController ()
- (IBAction)didClickBasid:(UIButton *)sender;
- (IBAction)didClickKeyFrame:(UIButton *)sender;
- (IBAction)didClickTranstion:(UIButton *)sender;
- (IBAction)didClickGroup:(UIButton *)sender;
@property (retain, nonatomic) IBOutlet UIView *myView;
@property (retain, nonatomic) IBOutlet UIImageView *imgView;
@property(nonatomic,assign)int index;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//将视图设置为 圆角
self.imgView.layer.cornerRadius = 30;
//将子图层超出的部分裁剪掉,如果使用了(注意:如果设置了这个属性,阴影设置是无效的)
// _imgView.layer.masksToBounds = YES;
//设置阴影
_imgView.layer.shadowColor = [UIColor orangeColor].CGColor;
//透明度
self.imgView.layer.shadowOpacity = 1.0;
//设置阴影偏移量
self.imgView.layer.shadowOffset = CGSizeMake(10, 10);
//模糊程度
self.imgView.layer.shadowRadius = 1.0;
#pragma mark -----------------设置myView的阴影和圆角
self.myView.layer.cornerRadius = 30;
self.myView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
self.myView.layer.shadowOpacity = 1.0;
self.myView.layer.shadowOffset = CGSizeMake(20, 20);
#pragma mark -----------------自定义CALayer
[self customLayer];
}
-(void)customLayer{
//创建layer对象
CALayer * myLayer = [CALayer layer];
//设置其位置和大小
myLayer.bounds = CGRectMake(0, 0, 80, 80);
//设置背景颜色
myLayer.backgroundColor = [UIColor yellowColor].CGColor ;
//设置锚点(定位点,默认是(0.0),谨记:锚点和position始终是重合的
myLayer.anchorPoint = CGPointMake(0, 0);
myLayer.position = CGPointMake(50, 50);
//添加到父图层
[self.view.layer addSublayer:myLayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_imgView release];
[_myView release];
[super dealloc];
}
#pragma mark ----------------Basic动画
- (IBAction)didClickBasid:(UIButton *)sender {
/*
//创建动画对象
CABasicAnimation * basicAnimation = [CABasicAnimation animation];
//告诉系统要执行行的动画,影响的是哪个属性
basicAnimation.keyPath = @"position";
#warning mark ----------如果不想让layer回来起始的位置,必须设置以下两个属性
basicAnimation.removedOnCompletion = NO;
//保存动画的最新状态
basicAnimation.fillMode = kCAFillModeForwards;
//设置时间
basicAnimation.duration = 10;
//设置通过动画,将layer从哪移动到那
basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
[self.imgView.layer addAnimation:basicAnimation forKey:@"basic"];
//没有改变position,所以还会回去,
NSLog(@"%@",NSStringFromCGPoint(_imgView.layer.position));
*/
//使用basic动画实现旋转
CABasicAnimation * basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform"];
//设置动画时间
basicAnimation1.duration = 1.0;
//设置最终值
basicAnimation1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2 * 2, 1, 1, 0)];
basicAnimation1.removedOnCompletion = NO;
basicAnimation1.fillMode = kCAFillModeForwards;
[self.imgView.layer addAnimation:basicAnimation1 forKey:@"basic1"];
//移除动画
[self.imgView.layer removeAnimationForKey:@"basic1"];
}
#pragma mark ==========帧动画
- (IBAction)didClickKeyFrame:(UIButton *)sender {
CAKeyframeAnimation * keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//设置动画时间
keyFrame.duration = 0.3;
//设置imageView抖动的角度
//角度转化为弧度 :度数/180 * M_PI
keyFrame.values = @[@(180.0*M_PI),@(8*M_PI),@(8*M_PI)];
//设置动画重复次数
keyFrame.repeatCount = 1000;
[self.imgView.layer addAnimation:keyFrame forKey:@"keyFrame"];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSTimer * time = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(didClickTranstion:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:time forMode:NSDefaultRunLoopMode];
do {
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20]];
} while (1);
}
-(void)picture:(NSTimer *)timer
{
[self didClickTranstion:nil];
}
#pragma mark ----------CATranstion(专场动画)
- (IBAction)didClickTranstion:(UIButton *)sender {
//属性
//type:设置动画过度的类型
//subType:动画过度的方向
//startProgress:动画的起点
//endProgress:动画的结束点
self.index++;
self.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",_index%19]];
//创建专场动画对象
CATransition * transition = [CATransition animation];
transition.type = @"cube";
transition.subtype = kCATransitionFromLeft;
//设置动画时间
transition.duration = 1.0;
transition.repeatCount = 1;
[self.imgView.layer addAnimation:transition forKey:@"transition"];
//帧动画
CAKeyframeAnimation * keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//设置动画时间
keyFrame.duration = 0.3;
//设置imageView抖动的角度
//角度转化为弧度 :度数/180 * M_PI
keyFrame.values = @[@(180.0*M_PI),@(8*M_PI),@(8*M_PI)];
//设置动画重复次数
keyFrame.repeatCount = 1;
//basic动画
CABasicAnimation * basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform"];
//设置动画时间
basicAnimation1.duration = 1.0;
//设置最终值
basicAnimation1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2 * 2, 1, 1, 0)];
basicAnimation1.removedOnCompletion = NO;
basicAnimation1.fillMode = kCAFillModeForwards;
CAAnimationGroup * animGroup = [CAAnimationGroup animation];
animGroup.duration = 3.0;
animGroup.animations = @[transition,keyFrame,basicAnimation1];
// [self.imgView.layer addAnimation:animGroup forKey:@"group"];
}
- (IBAction)didClickGroup:(UIButton *)sender {
// 平移动画
CABasicAnimation *a1 = [CABasicAnimation animation];
a1.keyPath = @"transform.translation.y";
a1.toValue = @(100);
// 缩放动画
CABasicAnimation *a2 = [CABasicAnimation animation];
a2.keyPath = @"transform.scale";
a2.toValue = @(0.0);
// 旋转动画
CABasicAnimation *a3 = [CABasicAnimation animation];
a3.keyPath = @"transform.rotation";
a3.toValue = @(M_PI_2);
//创建组动画
CAAnimationGroup * groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = @[a1,a2,a3];
groupAnimation.removedOnCompletion = NO;
groupAnimation.fillMode = kCAFillModeBackwards;
groupAnimation.duration = 1.0;
[self.imgView.layer addAnimation:groupAnimation forKey:@"group"];
}
@end