2010-10-11 5 views
2

Lorsque j'ai cliqué sur vue, créez un chemin en forme d'étoile.comment faire CALayer auto-tournante

Maintenant, je veux faire tourner l'étoile qui est ajoutée à CALayer.

Quelqu'un peut-il m'aider .... Comment faire tourner le calayer sur l'iphone.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
     UITouch *touch=[touches anyObject]; 
     currentPoint=[touch locationInView:self.view]; 
     CGMutablePathRef starPath; 
     rootLayer = [CALayer layer]; 
     rootLayer.frame = self.view.bounds; 
     [self.view.layer addSublayer:rootLayer]; 
     starPath = CGPathCreateMutable(); 
     CGPathMoveToPoint(starPath, NULL,currentPoint.x,currentPoint.y+15.0f); 
     for(int i = 1; i < 5; ++i) 
     { 
     CGFloat x =15.0*sinf(i * 4.0 * M_PI/5.0); 
     CGFloat y =15.0*cosf(i * 4.0 * M_PI/5.0); 
     CGPathAddLineToPoint(starPath, NULL,currentPoint.x+x,currentPoint.y+y); 
     } 
     CGPathCloseSubpath(starPath); 
     shapeLayer=[CAShapeLayer layer]; 
     shapeLayer.path = starPath; 
     fillColor = [UIColor colorWithHue:0.825 saturation:1 brightness:1 alpha:1.0]; 
     shapeLayer.fillColor = fillColor.CGColor; 
     [rootLayer addSublayer:shapeLayer]; 
     CGPathRelease(starPath); 
     float zDistance=30; 
     CATransform3D aTransform=CATransform3DIdentity; 
     aTransform.m34=1.0/-zDistance; 
     rootLayer.sublayerTransform=aTransform; 
     [self addAnimationToLayer:rootLayer]; 
    } 

    -(void)addAnimationToLayer:(CALayer *)layer 
    { 
    CABasicAnimation *theAnimation; 
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    theAnimation.fromValue=[NSNumber numberWithDouble:M_PI_2]; 
    //theAnimation.toValue=[NSNumber numberWithFloat:6.0f*M_PI/180.0f]; 
    theAnimation.duration=100; 
    // theAnimation.autoreverses=TRUE; 
    theAnimation.speed=100; 
    theAnimation.repeatCount=10; 
    [layer addAnimation:theAnimation forKey:@"transform"] ; 
    } 

Répondre

1

Utilisez CAKeyframeAnimation comme ceci:

CAKeyframeAnimation *spinAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
spinAnim.duration = 3.0; // some appropriate duration 

float firstHalfRotation = M_PI; 
float secondHalfRotation = (M_PI * 2.0); 

spinAnim.values = [NSArray arrayWithObjects: 
        [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0, 0, 1)], 
        [NSValue valueWithCATransform3D:CATransform3DMakeRotation(firstHalfRotation, 0, 0, 1)], 
        [NSValue valueWithCATransform3D:CATransform3DMakeRotation(secondHalfRotation, 0, 0, 1)], 
        nil]; 
[layer addAnimation:spinAnim forKey:@"spin"];