【问题标题】:Core Graphics animation is not smoothCore Graphics 动画不流畅
【发布时间】:2014-06-20 19:05:56
【问题描述】:

所以我使用 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];
}

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

这里有什么问题。

【问题讨论】:

  • 使用 CoreAnimation,而不是 UIView 动画块。这就是造成问题的原因。
  • 你为什么需要timer > 360 检查你是否打电话给[self rotateHand]
  • @random 我从 ios 7 中派生了这段代码,所以认为它不应该有问题
  • @VitalyS。改变提供的角度
  • 您的目标只是让某物在一个圆圈内制作动画?您是否需要能够暂停/停止它?为什么你的计时器运行@0.01 秒并调用一个名为callEverySecond 的方法?

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


【解决方案1】:

间隔为 0.01 的计时器希望每秒触发 100 次。 iOS 每秒只更新屏幕 60 次。

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

另外,屏幕更新的间隔是 1/60 = 0.01666667 秒。因此,您的 UIView 动画持续时间小于屏幕更新之间的时间,因此它不能产生任何可见的动画。摆脱你的UIView 动画。

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

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];

【讨论】:

  • 感谢您的详细回答。我在看一本书,所以我认为这应该是正确的轮换方式。
【解决方案2】:

NSTimer 的精度并不高。此外,将其调用为 100 次/秒对于动画目的来说太过分了。如果您想要每秒转一圈的平滑旋转,请改用重复的CAAnimation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 2010-11-07
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2018-07-28
    • 2015-11-28
    相关资源
    最近更新 更多