【问题标题】:How to rotate image in the speedometer?如何在车速表中旋转图像?
【发布时间】:2009-09-15 19:55:02
【问题描述】:
我正在使用此代码进行旋转动画:
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"];
在这段代码中,imageview 的底点是不固定的。我想设置图像,使其从图像底部的固定点旋转(如速度计)。
你能帮我解决这个问题吗?
【问题讨论】:
标签:
iphone
core-animation
【解决方案1】:
试试这样的:
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);
【解决方案2】:
这个问题现在已经很老了,但答案与锚点有关。您只需将图像视图的锚点设置为底部中心。这是显示不同锚点的指南。
因此,您可以将图层的锚点设置为 0.5、1.0,如下所示:
[[imgArrow layer] setAnchorPoint:CGPointMake(0.5, 1.0)];
【解决方案3】:
要从锚点旋转箭头,请使用以下代码:
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];