具有核心动画的非平滑动画

时间:2011-01-27 21:59:48

标签: iphone ios core-animation calayer clock

这是一个奇怪的请求,但我正在使用Core Animation(CALayers),我希望我的动画不稳定且不平滑。我想要一个我设置的图像像时钟上的秒针一样旋转。这是我的代码:

UIImage *arrowImage = [UIImage imageNamed:@"arrow.jpg"];

CALayer *arrow = [CALayer layer];
arrow.contents = (id)arrowImage.CGImage;
arrow.bounds = CGRectMake(0, 0, 169.25, 45.25);
arrow.position = CGPointMake(self.view.bounds.size.width / 2, arrowImage.size.height / 2);
arrow.anchorPoint = CGPointMake(0.0, 0.5);

[self.view.layer addSublayer:arrow];

CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim1.fromValue = [NSNumber numberWithFloat:0];
anim1.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
anim1.duration = 4.0;
[arrow addAnimation:anim1 forKey:@"transform"];

它会产生滑动,我不想要。我该如何解决这个问题? 任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

如果您希望它真的不稳定,请不要使用Core Animation。另一方面,如果你想要在这两个极端之间的某个地方,不要使用线性媒体计时。相反,您可能想尝试kCAMediaTimingFunctionEaseIn,以便动画在手移动时稍微加速。

答案 1 :(得分:0)

执行此操作的简单方法是简单地将变换应用于视图。秒针将从一个位置突然移动到下一个位置。只需每秒旋转360/60 = 6度。

如果您希望二手为每个刻度执行动画,您可以使用非常快速的UIView基于块的动画。 (比如持续1/15秒左右。)

看一下名字以animateWithDuration开头的UIView类方法。

这样的事情:

- (void) moveSecondHand;
{
  seconds++;
  angle = M_PI*2*seconds/60 - M_PI/2;
  CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
  [UIView animateWithDuration: 1.0/15
    animations: *{
      secondHand.transform = transform
    }];
}

这就是所需要的一切。你每秒都会用一个计时器触发代码。默认情况下,动画使用缓入,缓出时间,可以很好地模拟物理运动。尝试不同的持续时间,但1/15可能是一个很好的起点(你想要它快,但不要太快看到。)

如果你想要对你的动画进行摆动,你需要得到更多的动画,并创建一个动画组,首先将其移动全部量,然后做一个重复的动画,超过停止点少量,然后回去。

相关问题