Problem with rotating clock needle

Go To StackoverFlow.com

2

I'm a beginner in Objective-C and I'm trying to make a clock. I'm just testing at the moment and my problem is that for each loop the needle rotates from its initial position but not from the last position. So, my question is how to make the next rotation in the for loop from the last position the needle got?

Help me please, i'm doing my last school year internship. Here is my code:

#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
2009-06-16 09:50
by Albatrox
If cranTest is supposed to be pi, your value is wrong. It should be 3.1415927 or so.

Of course since you're setting it to an int variable, it doesn't matter because it will be truncated to 3 - David 2009-06-16 13:57

For representing pi, use the #define M_PI instead. See this question: http://stackoverflow.com/questions/978182/pi-in-objective- - Quinn Taylor 2009-06-16 18:54


2

Your code uses a timer to start a CAAnimation every second. You should rather use the repeatCount property of CAAnimation (from the CAMediaTiming protocol) to enable continuous animation.

Using the cumulative or additive properties, you can set up your animation to start the next loop from where the last stopped.

2009-06-16 10:50
by Nikolai Ruhe
nope, i've tried cumulative and/or additive property and for each loop increment my needle restart from its initial position but not not form where it stopped. That's my problem. Do you have any solution please. I'd like to make a clock in fact and thecode i provide is just to test rotatio - Albatrox 2009-06-17 08:08


0

CABasicAnimation (documentation here) is a subclass of CAPropertyAnimation (documentation here)

CAPropertyAnimation has cumulative, and additive

2009-06-16 13:34
by Ed Marty


0

I'm not sure, don't have experience with CoreAnimation, but I believe you have to set the byValue property:

Defines the value the receiver uses to perform relative interpolation.

So try:

spinAnimation.byValue = M_PI/6;

I'm not sure if you have to do an explicit cast to a float since the property is an id, I'm new to Objective-C as well. So I guess you would have to do:

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

I'm not sure you need the cummulative and additive properties set, try commenting them out:

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

And like David said as a comment to your original post, you will want to set cranTest to be a float instead of an integer, otherwise the value gets truncated to 3:

float cranTest = 3.1415279f;
2009-06-18 07:44
by Jorge Israel Peña


0

Try doing this instead:

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

I believe that shouldn't give any problems. And remember to make the other changes I told you about, like changing cranTest from being an int to being a float.

Also when you reply, don't add an answer, instead, add a comment to the answer. Uh.. répondre à ma réponse avec un comment, pas une autre reponse.

2009-06-19 05:33
by Jorge Israel Peña
Thank you blaenk i solved my problem on thursday:

spinMinuteHand.fromValue = [NSNumber numberWithFloat:cranDebutMinute]; cranDebutMinute += (0.1*degre); spinMinuteHand.toValue = [NSNumber numberWithFloat:cranDebutMinute];

good luck for your internet radio streaming app :° - Albatrox 2009-06-22 15:15

Ads