ios - 我想在 iOS 中按角度旋转图像

标签 ios animation uiimageview angle

http://screencast.com/t/OAb6BDNgiK

在上图中有一条白线,我只想根据角度在圆圈中为该图像设置动画。 假设如果我的角度是 20 度,那么白线会以 58 度动画,它会像下图一样

http://screencast.com/t/xuzngv8gRY

她是我的密码

 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"):

enter image description here

白线(命名为@"white-line"):

enter image description here

尝试这段代码:

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/

相关文章:

iphone - 调用另一个类中的方法失败

ios - 获取 UIButton 的大小并右对齐

ios - 消除 Objective C 中委托(delegate)的重复代码

jquery - 使用 jQuery 绘制动画线条

ios - 如何为所有分辨率制作全屏图像

iphone - 调整线条以适应 ImageView 中的头部

java - 节拍匹配算法

android - 用动画修改 TextView 的 MaxLines 属性?

ios - 如何通过拖动 ImageView 的右角来调整 ImageView 的大小和旋转 ImageView

iOS Swift - 改变背景颜色