项目更新,要做旋转动画,找到几种方法来实现,在此做下记录,以便日后使用方便!
1、 实现图片的自动旋转
如果你门项目要求很简单,就是想让它旋转一下,那么下面这个就很适合你!
根据设定的时间,设定的弧度自动旋转
CABasicAnimation* rotationAnimation;
//此处可根据需要设置x/y/z轴
rotationAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];
//fromValue 和 toValue 这两个参数控制着方向
rotationAnimation.fromValue =[NSNumbernumberWithFloat:M_PI/2];
rotationAnimation.toValue= [NSNumbernumberWithFloat:M_PI/2];
rotationAnimation.duration=0.3;
rotationAnimation.cumulative=NO;
rotationAnimation.repeatCount=0;
rotationAnimation.autoreverses=YES; //自动回放
[imageView.layeraddAnimation:rotationAnimationforKey:@"rotationAnimation"];
通过以上代码就可以设置一张图片的自动旋转啦!
但是,如果你要是旋转了180°,那么图片是反的,无解!!!图片对称没问题
2、 切换图片的旋转
在我们的项目中,要求了旋转后要更换图片,甚至界面,所以旋转升级了
1.构建两个视图,利用系统封装好的转换动画进行切换
核心代码 :
[UIViewtransitionFromView:fromView toView:toView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOLfinished) {
}];
//UIViewAnimationOptionTransitionFlipFromLeft这个属于转场形式,所以你可以自己选
代码示例:
使用:
_switchView= [[SwitchAnimaViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width-60,0,40,40*ONEHIGHT_VIEW)];
_switchView.backgroundColor= [UIColorcolorWithWhite:0.0alpha:1];
[commentMaskViewaddSubview:self.switchView];
UIView*backView=[[UIViewalloc]initWithFrame:_switchView.bounds];
UIView*frontView=[[UIViewalloc]initWithFrame:_switchView.bounds];
UIButton*shopB = [UIButtonnew];
[shopBsetImage:[UIImageimageNamed:@"img"]forState:UIControlStateNormal];
[shopBaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
shopB.frame=CGRectMake(0,0,40,40);
[backViewaddSubview:shopB];
_switchView.backView= backView;
UIButton*interactionB = [UIButtonnew];
[interactionBsetImage:[UIImageimageNamed:@"img2"]forState:UIControlStateNormal];
[interactionBaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
interactionB.frame=CGRectMake(0,0,40,40);
[frontViewaddSubview:interactionB];
_switchView.frontView= frontView;
- (void)btnClick:(UIButton*)sender{
_switchView.isBeginFlip=YES;
}
//封装
#import"SwitchAnimaView.h"
@implementationSwitchAnimaView
- (instancetype)initWithFrame:(CGRect)frame {
if(self=[superinitWithFrame:frame]) {
self.backView.backgroundColor= [UIColorcolorWithWhite:0.0alpha:0];
self.frontView.backgroundColor= [UIColorcolorWithWhite:0.0alpha:0];
self.goingToFrontView=NO;
}
returnself;
}
- (void)setFrontView:(UIView*)frontView {
if(!_frontView) {
_frontView=frontView;
[selfaddSubview:frontView];
[selfbringSubviewToFront:self.frontView];
}
}
- (void)setBackView:(UIView*)backView {
if (!_backView) {
_backView=backView;
}
}
- (void)setIsBeginFlip:(BOOL)isBeginFlip {
self.goingToFrontView= !self.goingToFrontView;
UIView*fromView =self.goingToFrontView?self.frontView:self.backView;
UIView*toView =self.goingToFrontView?self.backView:self.frontView;
[UIViewtransitionFromView:fromView toView:toView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOLfinished) {
}];
}
分析/注意:1.如果你要转这个布局中的某个视图,那么最好把封装到,因为弄不好你会把根视图旋转的
2.如果你的图片背景是透明的,那么效果不是很好
NO.3 可控制的旋转,切换图片
这种我是看到京东app中,商品图片右下角的1/4字样,旋转180°,变成2/4,图片仍然是正的, 并且它会跟着上面商品轮播的偏移值的大小改变图片的旋转角度。这让我绞尽脑汁,查了很多方法,但是后来仔细观察才知道,他正旋转了90°,逆向旋转90°
很简单就是利用了一句核心代码 :
imgView.layer.transform=CATransform3DMakeRotation(angle/2,0,1,0);
实现代码:
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor= [UIColorwhiteColor];
UIImage*imag = [UIImageimageNamed:@"image1"];
UIImageView*parentView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,150, imag.size.width+3, imag.size.height)];
parentView.image=imag;
parentView.tag=1000;
[self.viewaddSubview:parentView];
UIButton*button=[UIButtonbuttonWithType:(UIButtonTypeSystem)];
button.frame=CGRectMake(0,0,100,80);
[buttonsetImage:imagforState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(buttonClick)forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:button];
}
- (void)buttonClick{
b= !b;
if(!_timer) {
_timer= [NSTimertimerWithTimeInterval:0.05target:selfselector:@selector(timerAction)userInfo:nilrepeats:YES];
[[NSRunLoopmainRunLoop]addTimer:self.timerforMode:NSDefaultRunLoopMode];
}
}
- (void)timerAction{
UIImageView*parentView = [self.viewviewWithTag:1000];
if(angleM_PI-0.05) {
parentView.image=b?[UIImageimageNamed:@"image2"]:[UIImageimageNamed:@"image1"];
a=YES;
}
//a 用来控制旋转角度的
if(a==NO) {
angle+=0.1;
}
if(a==YES) {
angle-=0.1;
}
//CATransform3DMakeRotation(角度,x,y,z);
parentView.layer.transform=CATransform3DMakeRotation(angle/2,0,1,0);
if(angle<0.01) {
// 完成动画,重置
[_timerinvalidate];
_timer=nil;
angle=0;
a=NO;
}
}
搞定!!!
简单的旋转动画基本已经实现,Demo什么的就不放了!