2009-09-15 7 views
1

J'utilise ce code pour l'animation rotate:Comment faire pivoter l'image dans le compteur de vitesse?

CABasicAnimation *spinAnimation = [CABasicAnimation 
            animationWithKeyPath:@"transform.rotation.z"]; 
spinAnimation.fromValue =[NSNumber numberWithFloat:0]; 
spinAnimation.toValue = [NSNumber numberWithFloat:3.30]; 
spinAnimation.duration =2; 
[imgArrow.layer addAnimation:spinAnimation forKey:@"spinAnimation"]; 

En point bas de ce code imageview n'est pas fixé. Je veux configurer l'image de sorte qu'elle tourne à partir d'un point fixe sur le bas de l'image (comme un compteur de vitesse).

Pouvez-vous m'aider avec ceci?

Répondre

1

Essayez quelque chose comme ceci:

NSString *path = [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]; 
UIImage *img = [UIImage imageWithContentsOfFile:path]; 
CGImageRef image = img.CGImage; 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSaveGState(context); 

CGContextTranslateCTM(context, 365.0, 305); 
CGContextRotateCTM(context, angle); 
touchRect = CGRectMake(-10.0, -5.0, img.size.width, img.size.height); // (-10.0, -5.0) - arrow's rotation center 
CGContextDrawImage(context, touchRect, image); 

CGContextRestoreGState(context); 
+0

touchRect est non déclaré. Comment puis-je le déclarer? –

+0

CGRect touchRect; –

1

Cette question est assez vieux maintenant, mais la réponse a à voir avec le point d'ancrage. Vous devez juste définir le point d'ancrage pour que la vue d'image soit le centre inférieur. Voici un guide pour montrer les différents points d'ancrage. alt text

Vous définiriez votre point d'ancrage de la couche à 0,5, 1,0 comme celui-ci:

[[imgArrow layer] setAnchorPoint:CGPointMake(0.5, 1.0)]; 
0

Pour faire pivoter la flèche de l'utilisation du point d'ancrage de ce code:

self.button.layer.anchorPoint = CGPointMake(self.button.layer.anchorPoint.x, self.button.layer.anchorPoint.y*2); 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5f]; 
[self.button setTransform: CGAffineTransformMakeRotation((M_PI/180) * -40.0579987)]; 
[UIView commitAnimations]; 
Questions connexes