核心图形动画不流畅

时间:2014-06-20 19:05:57

标签: ios objective-c rotation core-graphics core-animation

所以我使用Core Graphics来旋转视图

-(void)viewDidLoad
{
[super viewDidLoad];
timer = 0.0f;
// Do any additional setup after loading the view, typically from a nib.

[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(callEverySecond) userInfo:nil repeats:YES];
}

-(void)callEverySecond
{
if (timer>360) {
    timer=0.0f;
    [self rotateHand:timer];
}
else
{
    timer++;
    [self rotateHand:timer];
}
}

-(void)rotateHand:(CGFloat)angle
{
[UIView animateWithDuration:0.01 animations:^{
    CGAffineTransform matrix = CGAffineTransformMakeRotation(angle*M_PI/180);
    [[self.rotate layer]setAffineTransform:matrix];
}];

[[self.rotate layer]needsDisplay];
}

现在的问题是旋转速度不均匀并且减速并随机加速。

这里有什么问题。

2 个答案:

答案 0 :(得分:2)

间隔为0.01的计时器要每秒发射100次。 iOS仅每秒更新屏幕60次。

使用CADisplayLink而不是使用NSTimer“手动”执行动画。每次屏幕更新时,显示链接会触发一次(默认情况下)并与屏幕更新同步触发。

此外,屏幕更新之间的间隔为1/60 = 0.01666667秒。因此,UIView动画持续时间小于屏幕更新之间的时间,因此无法生成任何可见动画。摆脱你的UIView动画。

如果您只想让图层连续旋转,则不必使用NSTimerCADisplayLink。相反,请在CABasicAnimation属性上使用transform.rotation

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.repeatCount = HUGE_VALF;
animation.duration = 3.6;
animation.fromValue = @0.0;
animation.toValue = @(2 * M_PI);
[self.rotate.layer addAnimation:animation forKey:animation.keyPath];

答案 1 :(得分:0)

NSTimer并不意味着具有非常好的精确度。此外,调用它100次/秒对于动画来说是过度杀伤。如果您想要每秒旋转一次,请使用重复的CAAnimation代替。

相关问题