2012年12月7日 星期五

Core Animation的基本使用(八)

將CABasicAnimation 動作組合起來一起的效果



實作如下

1. 在StoryBoard加上一個新個Button





2. 將mainViewController.m稍作修改
- (IBAction)translationX:(id)sender {
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    animation.toValue = [NSNumber numberWithFloat:100.0];
    //animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"translationX"]; // 每一個key都要不同,才會同時動作
   
}
- (IBAction)translationY:(id)sender {
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    animation.toValue = [NSNumber numberWithFloat:100.0];
    //animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"translationY"];

}
- (IBAction)translationZ:(id)sender {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.z"];
    animation.toValue = [NSNumber numberWithFloat:100.0];
    //animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"translationZ"];   
}
- (IBAction)scale:(id)sender {
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.toValue = [NSNumber numberWithFloat:2.0];
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"scale"];
}
- (IBAction)rotationX:(id)sender {
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    animation.toValue =  [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"rotationX"];

}
- (IBAction)rotationY:(id)sender {
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    animation.toValue =  [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 3.0;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
   
    [layer2 addAnimation:animation forKey:@"rotationY"];

}
- (IBAction)rotationZ:(id)sender {
   
   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue =  [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
    animation.fromValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(30))];
    animation.duration = 3.0;
    animation.fillMode=kCAFillModeForwards;
   
    animation.removedOnCompletion= YES;
    animation.autoreverses=YES;
   
    [layer2 addAnimation:animation forKey:@"rotationZ"];
 
}



- (IBAction)combineAnimation:(id)sender {
 

    // 串列呼叫就會有同時動的效果了   


    [self translationX:sender];
    [self translationY:sender];
    [self translationZ:sender];
   
    [self rotationX:sender];
    [self rotationY:sender];
    [self rotationZ:sender];
   
    [self scale:sender];
   
}


3. 執行結果











沒有留言: