0

嘿,那里需要你的帮助,

我正在尝试为我的 iPhone 创建一个模拟时钟。问题是,UIImage 没有以正确的方式移动或没有移动,大多数时候它在整个视图中旋转。是否缺少任何框架,我将度数转换为弧度......这是代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Getting a Timer
    NSTimer *timer = [[NSTimer alloc]init];
    timer = [NSTimer scheduledTimerWithTimeInterval: 1 target:self
                                           selector:@selector(tick:) userInfo:nil repeats:YES];
    [timer fire];
    self.ticker = 0;
    self.minutes = 0;
    self.hours = 0;
}

- (void)tick:(NSTimer *)theTimer
{
    //Turning Seconds into Minutes and Hours
    self.ticker ++;
    self.timerLabel.text = [NSString stringWithFormat:@"%d",self.ticker];
    self.seconds = self.ticker;
    if (self.seconds == 60){
        self.ticker = 0;
        self.seconds = 0;
        self.minutes ++;
    }
    if (self.minutes == 60){
        self.minutes = 0;
        self.hours ++;
    }

    //Main issue here...
    // 360° / 60 second = 6° per second
    double grad = self.ticker * 6;
    //Converting Grad into Rad
    double rad = M_PI * grad / 180;
    //Set Transformation ---> CGAffineTransformMakeRotation does not work eighter
    CGAffineTransform transform = CGAffineTransformRotate(self.pointerImage.transform, rad);
    //Set Transformation to UIImageView caleld pointerImage
    self.pointerImage.transform = transform;
    //self.pointerImage.center = CGPointMake(160, 420);
    //NSLog(@"%f", grad);



    if ( self.ticker == 60){
        self.ticker = 0;
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

1

围绕圆心的旋转需要初始变换来偏移边界 - 因此变换位于圆心。您一定会在此站点上找到示例代码。我强烈建议您创建一个单视图项目,为其添加一个彩色方形视图,并尝试使其正确旋转。只有这样才能获取该代码并将其集成到您的实际项目中。

于 2013-10-29T12:27:02.477 回答
0

转换是累积的,因此您只需要在每个刻度上添加增量更改:

CGAffineTransform transform = CGAffineTransformRotate(self.pointerImage.transform, (M_PI * 6 / 180));

正如您所拥有的那样,您的时钟指针在每个滴答 {1,3,6,10,15,21,28,36,45,55,6,18,31...} 上都会跳这么远,这就是为什么它看起来跳来跳去。

CGAffineTransform 类参考中的相关行是这样的,取自 CGAffineTransformRotate 方法定义:

您可以使用此函数通过向现有仿射变换添加旋转值来创建新的仿射变换矩阵。生成的结构表示一个新的仿射变换,您可以使用(如果需要,可以重复使用)来旋转坐标系。

于 2013-10-29T12:30:31.787 回答