How to rotate a CALayer by using Core Animation?

Discussion in 'iOS Programming' started by lamfan, Sep 30, 2008.

  1. lamfan macrumors newbie

    Joined:
    Aug 4, 2008
    #1
    [CABasicAnimation animationWithKeyPath:mad:""];

    I can't find out how to add the rotation by Core Animation.

    Anyone know how to do it?
     
  2. annguyen macrumors newbie

    Joined:
    Oct 2, 2008
    #2
    This is a simple sample code for you to reference...

    Code:
    CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
    	if (direction == ROTATE_FROM_LEFT) {
    		rotation.values = [NSArray arrayWithObjects:
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 1.0f, 0.0f)],
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f)],nil];
    	} else if (direction == ROTATE_FROM_RIGHT) {
    		rotation.values = [NSArray arrayWithObjects:
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f)],
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 1.0f, 0.0f)],nil];
    	} else {
    		rotation.values = [NSArray arrayWithObjects:
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 1.0f, 0.0f)],
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f)],
    						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI * 2, 0.0f, 1.0f, 0.0f)],nil];
    		duration *= 2;
    	}
    	
    	rotation.duration = duration;
    	rotation.delegate = self;
    	
    	[[self layer] addAnimation:rotation forKey:@"transform"];
    
    Hope this can help.
    :apple:
     

Share This Page