Ios 如何围绕UIView旋转它';s中心,有1分钟的持续时间,1秒的间隔和丝般光滑的动画?

Ios 如何围绕UIView旋转它';s中心,有1分钟的持续时间,1秒的间隔和丝般光滑的动画?,ios,objective-c,uiviewanimation,Ios,Objective C,Uiviewanimation,我正在尝试旋转一个视图。此UIView表示时钟的秒指针 我需要每秒钟更新一次。像这样: - (void)startUpdates { _updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(animatePointer) userInfo:nil repeats:YES]; } 在我需要的时候停下来,像这样: - (void)stopUpdates { [

我正在尝试旋转一个视图。此UIView表示时钟的秒指针

我需要每秒钟更新一次。像这样:

- (void)startUpdates {
    _updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(animatePointer) userInfo:nil repeats:YES];
}
在我需要的时候停下来,像这样:

- (void)stopUpdates {
    [_updateTimer invalidate];
    _updateTimer = nil;
}
- (void)animatePointer {

    NSDate *now = [NSDate date];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:now];

    float angleForSeconds = (float)[components second] / 60.0;

    [UIView animateWithDuration:1.0 animations:^(void){
        _pointerGlow.layer.transform = CATransform3DMakeRotation((M_PI * 2) * angleForSeconds, 0, 0, 1);
    }];

}
从一秒到下一秒,每一秒都设置动画,如下所示:

- (void)stopUpdates {
    [_updateTimer invalidate];
    _updateTimer = nil;
}
- (void)animatePointer {

    NSDate *now = [NSDate date];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:now];

    float angleForSeconds = (float)[components second] / 60.0;

    [UIView animateWithDuration:1.0 animations:^(void){
        _pointerGlow.layer.transform = CATransform3DMakeRotation((M_PI * 2) * angleForSeconds, 0, 0, 1);
    }];

}
这确实有效,但并不顺利。它每秒钟停止一小部分,就像一个典型的挂钟一样。很好,但不是我想要的


有什么方法可以让这个动画变得丝般光滑吗?

您可以使用一个与60/秒的显示刷新率相关的CADisplayLink来尝试。代码如下所示(请注意,我没有添加任何逻辑来停止显示链接,但我加入了您想要调用的方法):

#导入“ViewController.h”
#进口
@界面视图控制器()
@属性(非原子,强)CADisplayLink*displayLink;
@属性(非原子)CFTimeInterval firstTimestamp;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
[自启动显示链接];
}
-(无效)startDisplayLink{
self.displayLink=[CADisplayLink displayLinkWithTarget:self-selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
}
-(无效)停止显示链接{
[self.displayLink失效];
self.displayLink=nil;
}
-(无效)handleDisplayLink:(CADisplayLink*)displayLink{
如果(!self.firstTimestamp)self.firstTimestamp=displayLink.timestamp;
NSTimeInterval经过=(displayLink.timestamp-self.firstTimestamp);
self.arrow.layer.transform=CATTransformorM3dMakeRotation((M_PI*2)*经过/60,0,0,1);
}

如果动画持续时间为1秒,为什么不进行一次连续动画?听起来你根本不想让指针停在第二个标记处,对吧?既然你质疑这个问题,我想我需要间隔,从程序上说。因为我需要停止和恢复动画从同一个地方或不是,在任何时候。但我真的不知道。谢谢@Firozelafeerd你知道为什么handleDisplayLink在失效后一直被呼叫吗?我在乐器中看到了它。有时需要1毫秒。。不经常。我还尝试手动从RunLoop中删除(通常,在使用displayLink之后,很多操作都发生在仪器中)。为什么会这样?谢谢@不,我不能解释。使其无效后,应将其从运行循环中删除,如果将其设置为nil,则应取消分配。