核心动画非流畅动画
问题描述:
这是一个奇怪的请求,但我使用核心动画(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"];
它会产生一个我不想要的滑动运动。我如何解决这个问题? 任何帮助表示赞赏。
答
如果你想它是真的不连贯,不要使用核心动画。另一方面,如果你想要在这两个极端之间的某个地方,不要使用线性媒体定时。相反,您可能想尝试kCAMediaTimingFunctionEaseIn
,以便随着手的移动动画略微加速。
答
这样做的简单方法是简单地将变换应用于视图。第二只手会从一个位置跳到另一个位置。每秒只需将旋转角度改变360/60 = 6度。
如果您希望二手为每个刻度做一个动画,您可以使用一个非常快速的UIView基于块的动画。 (比如用1/15秒左右的时间)。
看看UIView类的方法谁的名字以animateWithDuration开头。
事情是这样的:
- (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可能是一个很好的起点(你希望它快,但看不到太快)。
如果你想摆动动画,你需要更多的发明,并创建一个动画组,首先将其全部移动,然后制作一个重复动画,将停止点过量少量,然后返回。
感谢您的回复,虽然我听说使用CALayers会加快应用程序的性能。此外,我找到了一个我正在寻找的例子,尽管作者没有很好地解释它:http://blog.moonshine-project.com/en/2010/03/14/implicit-animation-of -custom-calayer-properties/ – 2011-01-27 23:14:37