ios - 无法再次翻转图层、CALayer、CATransform3D 旋转。

标签 ios objective-c calayer catransform3drotate

我想做的是:

  1. 单击按钮进行垂直翻转。
  2. 再次单击按钮,图层会将其翻转回来,依此类推...

代码如下:

    @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;
    }

但是,在步骤 1 中,我可以看到图层翻转 180 度,当我再次单击按钮时,没有任何反应。

我是不是中间漏掉了什么?请帮忙。

最佳答案

下面的代码仅获取发件人的选定状态。并且状态只设置一次。检查下面修改后的代码。

// 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;
    }

关于ios - 无法再次翻转图层、CALayer、CATransform3D 旋转。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15893144/

相关文章:

cocoa - CALayer 过滤器覆盖其他子层

ios - 带有自定义 ProgressImage 的 UIProgressView 拉伸(stretch)不正确。怎么修?

iphone - CALayer 的 display/drawRect 方法中到底应该发生什么?

android - Ionic2 proxyUrl 在设备上不起作用

iphone - 加载时添加一个小漩涡图标

objective-c - 数组未到达 cellForRowAtIndexPath

ios - 滑动手势识别器对我不起作用

ios - 无限 CABasicAnimation 停止

ios - 使用 Firebase 应用程序打开 url 选项的 GoogleSignIn 未调用

ios - UIScrollView 在 ios6 中不滚动