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

时间:2013-07-03 09:47:18

标签: 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];
}

5 个答案:

答案 0 :(得分:5)

背景图片(名为@“背景”):

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

答案 1 :(得分:1)

试试这个:

-(void) rotateImage:(float)angleRadians{

    self.transform = CGAffineTransformMakeRotation(angleRadians);
    CATransform3D rotatedTransform = imgMain.layer.transform;
    imgMain.layer.transform = rotatedTransform;    
}

也看到了这个:

  1. iphone : How to rotate a rectangle image with touch events?
  2. Rotate a UIImage or UIView to a particular angle, NOT an amount of angle

答案 2 :(得分:1)

试试这个

 [UIView animateWithDuration:0.50 animations:^{
            CGFloat angle = 1.5707f; // set angle as per your requirement
            tapview.transform = CGAffineTransformRotate(tapview.transform, angle);

        }];

答案 3 :(得分:0)

试试这个:     拍摄alpha为0的图像,除了那条白线并将图像放在彩色标尺上[必须开发适合图像的图像],现在采用whiteGaugeIndicator,您可以使用在LOC下面:

whiteGaugeIndicator.transform = CGAffineTransformMakeRotation(M_PI/9);

答案 4 :(得分:0)