Question

Thanks for the help from my first post at here so that I can do a vertical flip and the code looks like

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

}

My question is how can do this flip with animation. I did try to use

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

but still does not work. I'm not so sure that animation block is a good idea for this animation.

Do you guys have any ideas about this.

Was it helpful?

Solution

Use a CATransaction to change the implicit defaults

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

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

[CATransaction commit];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top