Cannot flip layer again,CALayer,CATransform3DRotate.

喜夏-厌秋 提交于 2019-12-11 19:18:45

问题


What I am trying to do is to do :

  1. Click a botton to do a vertical flip.
  2. Click a botton again, the layer wil flip it back and so on...

Codes are following :

    @interface ViewController (){
        CALayer *plane;
    }
    @end

    @implementation ViewController

    -(void)viewDidLoad
    {
        [super viewDidLoad];

        [self addALayer];


    }
    - (void)addALayer{


        plane                   =   [CALayer layer];
        plane.backgroundColor   =   [[UIColor orangeColor] CGColor];
        //[plane insertSublayer:normalBackground atIndex:0];

        plane.opacity           =   1;
        plane.frame             =   CGRectMake(0, 0, 300, 100);
        plane.position          =   CGPointMake(250, 150);
        plane.anchorPoint       =   CGPointMake(0.5, 0.5);
        plane.borderColor       =   [UIColor whiteColor].CGColor;
        plane.borderWidth       =   3;
        plane.cornerRadius      =   10;
        [self.view.layer addSublayer:plane];

    }
- (IBAction)click:(id)sender {
        BOOL isClicked                  =   ((UIButton*)sender).selected;
        ((UIButton*)sender).selected    =   !((UIButton*)sender).selected;
        CATransform3D transfrom         =   CATransform3DIdentity;
        transfrom.m34                   =   -1.0/ 500;
        if ( !isClicked ) 
            transfrom                   =   CATransform3DRotate(transfrom, degToRad(180.0), 1, 0, 0);
        else 
            transfrom                   =   CATransform3DRotate(transfrom, degToRad(-180.0), 1, 0, 0);

        plane.transform                 =   transfrom;
    }

However, at step 1, I can see the layer flip 180 degree and when I click the button again, there is nothing happening.

Did I miss something in the middle? Please help.


回答1:


The code below gets the sender's selected state only. And the state is only set once. Check the modified code below.

// Have a global variable
 BOOL isClicked 

- (IBAction)click:(id)sender {
        CATransform3D transfrom         =   CATransform3DIdentity;
        transfrom.m34                   =   -1.0/ 500;
        if ( !isClicked ) {
            transfrom                   =   CATransform3DRotate(transfrom, degToRad(180.0), 1, 0, 0);
        isClicked                       =   true;
        }
        else{ 
            transfrom                   =   CATransform3DRotate(transfrom, degToRad(360.0), 1, 0, 0); // OR CAN ALSO BE 0
        isClicked                       =   false;
        }
        plane.transform                 =   transfrom;
    }


来源:https://stackoverflow.com/questions/15893144/cannot-flip-layer-again-calayer-catransform3drotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!