旋转时钟针的问题

时间:2009-06-16 09:51:00

标签: objective-c iphone

我是Objective-C的初学者,我正在尝试制作一个时钟。我现在正在测试,我的问题是针对每个循环,针从其初始位置旋转而不是从最后位置旋转。那么,我的问题是如何从针的最后位置开始for循环中的下一次旋转?

请帮帮我,我正在上学一年。这是我的代码:

#import "MainView.h"
#include < math.h>

static inline double radians (double degrees) {return degrees * M_PI/180;}

@implementation MainView

int cranTest = 3.1415279;
int tps = 0; 

@synthesize aiguille;
@synthesize tempsEcoule;
@synthesize timer;


- (IBAction)go {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTime) userInfo:nil repeats:YES];
}

-(void)checkTime{
    if(tps < 12){
        [self updateLabel];
        [self tourne];
    }else{
        [timer invalidate];
        tempsEcoule.text = @"terminé";
    }
}

-(void)updateLabel{
    tps += 1;
    tempsEcoule.text = [NSString stringWithFormat:@"%i",tps];
}

-(void)tourne{      
    CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    spinAnimation.fromValue = 0;
    spinAnimation.toValue = [NSNumber numberWithFloat:cranTest];
    spinAnimation.duration = .5;

    [aiguille.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
}

@end

4 个答案:

答案 0 :(得分:2)

您的代码使用计时器每秒启动一次CAAnimation。您应该使用CAAnimation的repeatCount属性(来自CAMediaTiming协议)来启用连续动画。

使用累积或附加属性,您可以设置动画以开始上次停止的下一个循环。

答案 1 :(得分:0)

CABasicAnimationdocumentation here)是CAPropertyAnimationdocumentation here)的子类

CAPropertyAnimation具有累积性和附加性

答案 2 :(得分:0)

我不确定,没有CoreAnimation的经验,但我相信你必须设置byValue属性:

  

定义接收器用于执行相对插值的值。

所以试试:

spinAnimation.byValue = M_PI/6;

我不确定你是否必须对float进行显式转换,因为属性是id,我也是Objective-C的新手。所以我想你必须这样做:

spinAnimation.byValue = (float)(M_PI/6); // not sure if this is needed or correct

我不确定你是否需要设置累积和附加属性,尝试将它们注释掉:

// spinAnimation.cumulative = YES;
// spinAnimation.additive = YES;

就像David对你原来的帖子所做的评论一样,你需要将cranTest设置为浮点而不是整数,否则该值会被截断为3:

float cranTest = 3.1415279f;

答案 3 :(得分:0)

尝试这样做:

spinAnimation.byValue = [NSNumber withFloat:(M_PI/6)];

我认为不应该给任何问题。并记住做出我告诉你的其他改变,比如将cranTest从int改为浮动。

此外,当您回复时,请不要添加答案,而是在答案中添加评论。呃.. répondreàmaréponseaveveun comment,pas une autre reponse。