1

感謝從我在here的第一篇文章的幫助,使我可以做一個垂直翻轉和代碼看起來像如何使用CATransform3DRotate

@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

使用CATransaction改變隱含的默認值

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

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

[CATransaction commit]; 
+0

就是這麼整齊。感謝那.... – tranvutuan 2013-04-09 14:43:23