您的位置:首页 > 移动开发 > IOS开发

IOS开发:动画2 AlayerAnimation

2015-10-09 18:52 495 查看
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *currentView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,strong)NSTimer * timer;
//标记第几张图片
@property(nonatomic,assign)NSInteger index;

//@property(nonatomic,strong)NSString * name;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//设置圆角
self.currentView.layer.cornerRadius = 25;
//将超出范围的子图层裁剪掉 (如果设置成YES,对阴影设置不起作用)
//    self.currentView.layer.masksToBounds = YES;
//设置阴影颜色
[self.currentView.layer setShadowColor:[UIColor grayColor].CGColor];
//设置阴影的偏移量
[self.currentView.layer setShadowOffset:CGSizeMake(-20, 20)];
//设置透明度
[self.currentView.layer setShadowOpacity:1.0];
//模糊程度
[self.currentView.layer setShadowRadius:8];

//创建layer层
[self creatLayer];

}

-(void)creatLayer{
//初始化一个layer对象
CALayer * layer = [[CALayer alloc]init];
//给layer一个大小
layer.bounds = CGRectMake(0, 0, 100, 100);
//设置背景色
layer.backgroundColor = [UIColor redColor].CGColor; //这里记得加CGColor  否则报错

//锚点
layer.anchorPoint = CGPointMake(1, 1);
//定位点
layer.position = CGPointMake(50, 50);

//先找定位点,然后将锚点与定位点重合 即可确定view位置

//添加到父layer上面
[self.view.layer addSublayer:layer];

}

//basic动画
- (IBAction)CABasicAnimation:(UIButton *)sender {

//初始化一个对象
CABasicAnimation * basic = [CABasicAnimation animation];
//设置要实现动画的属性
//平移动画
basic.keyPath = @"position";
//设置从一个点移动到另一个点
basic.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
basic.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];

//设置动画时间
basic.duration = 2.0f;

//如果想保存动画后的位置,必须实现下面两个设置
basic.removedOnCompletion = NO;
basic.fillMode = kCAFillModeForwards;

//把动画添加到图片的layer层
[self.imageView.layer addAnimation:basic forKey:@"basic"];

//
CABasicAnimation *basic1 = [CABasicAnimation animation];
//设置旋转动画
basic1.keyPath = @"transform";
//度数转弧度公式:度数 * π/180
basic1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(90*M_PI/180, 1, 1, 1)];
//设置动画时间
basic1.duration = 2.0f;
[self.imageView.layer addAnimation:basic1 forKey:@"basic1"];

//根据key删除对应动画
[self.imageView.layer removeAnimationForKey:@"basic1"];

}

//帧动画
- (IBAction)CAKeyFrameAnimation:(UIButton *)sender {

//实例化一个帧动画对象
CAKeyframeAnimation * keyFrame = [CAKeyframeAnimation animation];
//设置要实现的动画属性
keyFrame.keyPath = @"transform.rotation";
keyFrame.values = @[@(-4*M_PI/180),@(4*M_PI/180),@(-4*M_PI/180)];
//设置动画时间
keyFrame.duration = 1.0f;
//设置重复次数
keyFrame.repeatCount = 5;
[self.imageView.layer addAnimation:keyFrame forKey:@"keyFrame"];

}

//转场动画
- (IBAction)CATranstionAnimation:(UIButton *)sender {

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

}

/*
1.	animation.type = @"cube"
2.	animation.type = @"suckEffect";
3.	animation.type = @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一种效果
4.	animation.type = @"rippleEffect";
5.	animation.type = @"pageCurl";
6.	animation.type = @"pageUnCurl"
7.	animation.type = @"cameraIrisHollowOpen ";
8.	animation.type = @"cameraIrisHollowClose ";  
*/
-(void)timerAction{

//
self.index++;
if (self.index == 19) {
self.index = 0;
}
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%ld.jpg",self.index]];

//首先申明一个转场动画对象
CATransition * transtion = [CATransition animation];
//设置转场动画类型
transtion.type = @"rippleEffect";
//设置转场动画方向
transtion.subtype = kCATransitionFromRight;
//动画开始位置
transtion.startProgress = 0;
//动画结束位置
transtion.endProgress = 1;
transtion.duration = 1;
[self.imageView.layer addAnimation:transtion forKey:@"transtion"];

}

//组动画
- (IBAction)CAAnimationGroup:(UIButton *)sender {

CABasicAnimation * basic1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];

basic1.toValue = @(100);

CABasicAnimation * basic2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

basic2.toValue = @(0.5);
//旋转
CABasicAnimation * basic3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
basic3.toValue = @(M_PI);
//初始化动画组
CAAnimationGroup * group = [CAAnimationGroup animation];
//设置动画组里面的元素
group.animations = @[basic1,basic2,basic3];
//设置动画时间
group.duration = 2.0f;

group.removedOnCompletion = NO;

group.fillMode = kCAFillModeForwards;

[self.imageView.layer addAnimation:group forKey:@"group"];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: