如何使用CATransform3DRotate进行翻转时添加持续时间

时间:2013-04-09 14:31:05

标签: objective-c core-animation uiviewanimation catransform3drotate

感谢我在here的第一篇文章的帮助,以便我可以进行垂直翻转,代码看起来像

  @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);
        plane.transform             =   transfrom;
    }
    else 
        plane.transform                 =   transfrom;

}

我的问题是如何通过动画进行翻转。我确实尝试过使用

UIView animateWithDuration(NSTimeInterval) delay:(NSTimeInterval) options:(UIViewAnimationOptions) animations:^(void)animations completion:(BOOL finished)completion

但仍然无效。我不太确定动画块是这个动画的好主意。

你们对此有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使用CATransaction更改隐式默认值

[CATransaction begin];
[CATransaction setAnimationDuration:1]; // in seconds

// Change layer animatable properties
plane.transform = transform;    

[CATransaction commit];