沿y轴的空间旋转动画!!

来源:互联网 发布:中世纪2 优化9 拜占庭 编辑:程序博客网 时间:2024/05/16 19:25

项目更新,要做旋转动画,找到几种方法来实现,在此做下记录,以便日后使用方便!

NO.1 实现图片的自动旋转

       如果你门项目要求很简单,就是想让它旋转一下,那么下面这个就很适合你

       根据设定的时间,设定的弧度自动旋转

    CABasicAnimation* rotationAnimation;

    //此处可根据需要设置x/y/z轴

    rotationAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];

    //fromValue 和 toValue 这两个参数控制着方向

    rotationAnimation.fromValue =[NSNumber numberWithFloatM_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°,那么图片是反的,无解!!!图片对称没问题

NO.2  切换图片的旋转

   在我们的项目中,要求了旋转后要更换图片,甚至界面,所以旋转升级了

   1.构建两个视图,利用系统封装好的转换动画进行切换

   核心代码 :

[UIViewtransitionFromView:fromView  

                        toView:toView

                      duration:1

                       options:UIViewAnimationOptionTransitionFlipFromLeft //这个属于转场形式,所以你可以自己选择

                    completion:^(BOOL finished) {

    }];

代码示例:

使用:

{

    _switchView = [[SwitchAnimaViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width - 600,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];

    [shopB setImage:[UIImageimageNamed:@"img"]forState:UIControlStateNormal];

    [shopB addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    shopB.frame =CGRectMake(0,0,40,40);

    [backView addSubview:shopB];

    _switchView.backView = backView;

    

    UIButton *interactionB = [UIButtonnew];

    [interactionB setImage:[UIImageimageNamed:@"img2"]forState:UIControlStateNormal];

    [interactionB addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    interactionB.frame =CGRectMake(0,0,40,40);

    [frontView addSubview:interactionB];

    _switchView.frontView = frontView;

}

- (void)btnClick:(UIButton *)sender{

    

    _switchView.isBeginFlip =YES;

}



//封装

#import "SwitchAnimaView.h"

@implementation SwitchAnimaView

-(instancetype)initWithFrame:(CGRect)frame

{

    if (self=[super initWithFrame:frame]) {

        self.backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0];

        self.frontView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0];

        self.goingToFrontView=NO;

    }

    return self;

}

-(void)setFrontView:(UIView *)frontView

{

    if (!_frontView) {

        _frontView=frontView;

        [self addSubview:frontView];

        [self bringSubviewToFront: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;

    [UIView transitionFromView:fromView

                        toView:toView

                      duration:1

                       options:UIViewAnimationOptionTransitionFlipFromLeft

                    completion:^(BOOL finished) {

    }];

}

   分析/注意: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);

    [button setImage:imagforState:UIControlStateNormal];

    [button addTarget: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 (angle <M_PI +0.05&&angle>M_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什么的就不放了,网上搜一堆一堆的!!

    另,这个文章是小猴子第一次写的博客,技术手段都不会用,无法上动图,代码也只是截取了部分核心代码,复制粘贴的话需要你添加些变量!!!






0 0