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
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.
CABasicAnimation
(documentation here) is a subclass of CAPropertyAnimation
(documentation here)
CAPropertyAnimation
has cumulative, and additive
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;
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.
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
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