在上图中有一条白线,我只想根据角度在圆圈中为该图像设置动画。 假设如果我的角度是 20 度,那么白线会以 58 度动画,它会像下图一样
她是我的密码
UIImageView *imgBenchmark = [[UIImageView alloc]initWithFrame:CGRectMake(153,70, 1, 26)];
self.benchmark = imgBenchmark;
[imgBenchmark release];
self.benchmark.layer.anchorPoint = CGPointMake(self.benchmark.layer.anchorPoint.x, self.benchmark.layer.anchorPoint.y*2);
self.benchmark.backgroundColor = [UIColor clearColor];
self.benchmark.image = [UIImage imageNamed:@"line.png"];
[self.view addSubview:self.benchmark];
[self rotateIt1:20];
-(void) rotateIt1:(float)angl
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.01f];
[self.benchmark setTransform: CGAffineTransformMakeRotation((M_PI / 9) *angl)];
[UIView commitAnimations];
}
最佳答案
背景图片(命名为@"Background"):
白线(命名为@"white-line"):
尝试这段代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UIImageView *whiteLine;
@end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.2
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(55, 20, 209, 105)];
background.image = [UIImage imageNamed:@"Background"];
[self.view addSubview:background];
self.whiteLine = [[UIImageView alloc]initWithFrame:CGRectMake(156, 20, 7, 210)];
self.whiteLine.image = [UIImage imageNamed:@"white-line"];
[self.view addSubview:_whiteLine];
[self rotateIt1:20];
}
-(void) rotateIt1:(float)angl
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
self.whiteLine.transform = CGAffineTransformMakeRotation((M_PI / 180) * angl);
[UIView commitAnimations];
}
这对我有用。
源代码:http://speedy.sh/wUZQm/Speedometer.zip
或者有一个自定义插件:https://github.com/sabymike/MSSimpleGauge
关于ios - 我想在 iOS 中按角度旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444735/