我正在开发一个 iPad 应用程序,我试图在其中显示秒针显示秒针运动,就像这样每秒更新一次。
- (void) updateClock:(NSTimer *)theTimer{
dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
seconds = [dateComponents second];
minutes = [dateComponents minute];
hours = [dateComponents hour];
secAngle = Degrees2Radians(seconds/60.0*360);
minAngle = Degrees2Radians(minutes/60.0*360);
hourAngle = Degrees2Radians(hours/24.0*360) + minAngle/24.0;
secHand.transform = CATransform3DMakeRotation (secAngle+M_PI, 0, 0, 1);
hourHand.transform = CATransform3DMakeRotation (hourAngle+M_PI, 0, 0, 1);
minHand.transform = CATransform3DMakeRotation (minAngle+M_PI, 0, 0, 1);
}
- (void)start{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];
}
然而,它现在的工作方式,它表明它每秒都在滴答作响。但我真正想要的是为图层设置动画,使其看起来在 1 分钟内从 0 到 360 的完整旋转中连续运动,但是,我只能让它开始从 0 到 360 做一个完整的旋转,所以我用了以下秒针:
-(void)startSmooth{
started = YES;
CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 60.0f;
fullRotation.repeatCount = HUGE_VALF;
[secHand addAnimation:fullRotation forKey:@"360"];
}
然而,这样做的问题是它总是以 0 度角开始旋转,因此无论时间是什么,指针看起来似乎总是从 30 秒开始运动。
我想做的是让它从秒针应该在的实际点开始。我怎样才能做到这一点?