定义属性
- (IBAction)trainsformAction:(id)sender;
@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,assign)BOOL isRotation;
@property(nonatomic,retain)UIImageView *secImageView;
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,assign)BOOL isSelect;
@property(nonatomic,retain)UIButton *goodButton;
@property(nonatomic,retain)UIView *myView;
创建
self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
self.imageView.layer.cornerRadius=100;
self.imageView.layer.masksToBounds=YES;
[self.view addSubview:self.imageView];
self.imageView.image=[UIImage imageNamed:@"c2.jpg"];
//用户交互打开
self.imageView.userInteractionEnabled=YES;
1. 音乐播放器转啊转
//layer动画(音乐播放器转啊转)
//控件可以拆分成两部分,一部分是控件的样式,比如边框,文字等,这些都是通过操作控件layer层来完成的,另一部分是控件的功能,比如button点击方法,这些都是自己的功能
//layer动画主要是给控件layer层添加动画来实现效果
//参数需要指定修改的是哪个属性
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//设置动画起始的值
animation.fromValue=[NSNumber numberWithFloat:0];
//设置最终旋转的角度
animation.toValue=[NSNumber numberWithFloat:M_PI*2];
//缩放 (把上面三行注掉换成缩放,动画就由转转变成缩放)
// CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
// //起始缩放比例
// animation.fromValue=[NSNumber numberWithInt:1];
// animation.toValue=[NSNumber numberWithInt:2];
//设置动画的播放时长
animation.duration=10;
//设置动画执行次数,NSIntegerMax整数最大值
animation.repeatCount=NSIntegerMax;
//旋转之后是否回到原来位置
animation.autoreverses=NO;
//是否按照结束位置继续旋转
animation.cumulative=YES;
//把动画加到imageview的layer上
[self.imageView.layer addAnimation:animation forKey:@"rotate"];
//给imageview上添加一个轻点的手势
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.imageView addGestureRecognizer:tap];
self.isRotation=NO;
旋转的实现方法(暂停和播放)
-(void)tapAction:(UITapGestureRecognizer *)tap{
if (self.isRotation==NO) {
//每一个view的layer层系统都有一个记录时间的属性,通过改变view的动画时间来控制动画效果
//获取播放旋转的时间点
CFTimeInterval stop=[self.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
//为了暂停,把播放速度设置为0
self.imageView.layer.speed=0;
//记录当前时间的偏移量
self.imageView.layer.timeOffset=stop;
}else{
//先找到上一次停止的时间偏移量
CFTimeInterval time=self.imageView.layer.timeOffset;
//让动画旋转的速度恢复
self.imageView.layer.speed=1;
//把偏移量清零
self.imageView.layer.timeOffset=0;
self.imageView.layer.beginTime=0;
//接下来以停止时间作为开始,继续旋转
self.imageView.layer.beginTime=[self.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil]-time;
}
//状态取反
self.isRotation=!self.isRotation;
}
2. 系统的多种动画效果
self.secImageView=[[UIImageView alloc] initWithFrame:CGRectMake(70, 450, 200, 200)];
[self.view addSubview:self.secImageView];
self.secImageView.image=[UIImage imageNamed:@"c2.jpg"];
self.button=[UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame=CGRectMake(300, 450, 50, 50);
self.button.backgroundColor=[UIColor cyanColor];
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
点击方法
-(void)click:(UIButton *)button{
CATransition *transition=[CATransition animation];
//设置动画类型
transition.type=@"pageCurl";
/** type
*
* 各种动画效果 其中除了'fade', `moveIn', `push' , `reveal' ,其他属于私有的API.
* ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
* @"cube" 立方体翻滚效果
* @"moveIn" 新视图移到旧视图上面
* @"reveal" 显露效果(将旧视图移开,显示下面的新视图)
* @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果)
* @"pageCurl" 向上翻一页
* @"pageUnCurl" 向下翻一页
* @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
* @"rippleEffect" 滴水效果,(不支持过渡方向)
* @"oglFlip" 上下左右翻转效果
* @"rotate" 旋转效果
* @"push"
* @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向)
* @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向)
*/
//设置动画时长
transition.duration=3;
//设置动画播放次数
transition.repeatCount=NSIntegerMax;
[self.secImageView.layer addAnimation:transition forKey:@"transition"];
}
3. 长按抖动
//长按抖动
self.myView=[[UIView alloc] initWithFrame:CGRectMake(110, 140, 160, 50)];
self.myView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myView];
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.myView addGestureRecognizer:longPress];
点击方法
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
//关键帧动画
CAKeyframeAnimation *keyAnimation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
float top=M_PI/20;
float bom=M_PI/20;
keyAnimation.values=@[@(top),@(0),@(bom),@(0),@(top)];
//设置动画时长
keyAnimation.duration=0.1;
//次数
keyAnimation.repeatCount=NSIntegerMax;
[self.myView.layer addAnimation:keyAnimation forKey:@"key"];
//停止动画
//dispatch_after的作用就是会延迟执行内容
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//在block里写停止动画的方法
[self.myView.layer removeAnimationForKey:@"key"];
});
}
4. 点赞效果
self.goodButton=[UIButton buttonWithType:UIButtonTypeCustom];
self.goodButton.frame=CGRectMake(300, 70, 50, 50);
[self.view addSubview:self.goodButton];
[self.goodButton addTarget:self action:@selector(changeImage:) forControlEvents: UIControlEventTouchUpInside];
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal ];
//把button自带的点击效果取消,button的类型要设置成custom
self.goodButton.adjustsImageWhenHighlighted=NO;
self.isSelect=NO;
点击实现
-(void)changeImage:(UIButton *)button{
//关键帧动画
//用动画完成放大的效果
CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
//需要给他设置一个关键帧的值,这个值就是变化过程
//values是一个数组
animation.values=@[@(0.5),@(1.0),@(1.5)];
//设置动画的时长
animation.duration=0.2;
//加到button上
[self.goodButton.layer addAnimation:animation forKey:@"animation"];
//根据状态更换图片
if (self.isSelect == NO) {
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconHgood.png"] forState:UIControlStateNormal];
}else{
[self.goodButton setBackgroundImage:[UIImage imageNamed:@"IconGood.png"] forState:UIControlStateNormal];
}
self.isSelect=!self.isSelect;
}