核心动画是这类事情的一个很好的解决方案。您有cashapelayer,它允许您根据路径绘制形状,并且您可以使用基本动画或关键帧动画制作动画。您可以在按钮中执行类似的操作单击:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)]];
[animation setToValue:[NSValue valueWithCGPoint:CGPointMake(320.0, 480.0)]];
[animation setDuration:2.0f];
[shapeLayer addAnimation:animation forKey:@"positionAnimation"];
这将使图层在两秒钟内从点0.0、0.0(左上角)到320.0、480.0(右下角)。将动画添加到层时,它将立即开始播放。如果要旋转动画(不确定是否从文章中旋转),可以执行以下操作:
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation
animationWithKeyPath:@"transform.rotation.z"];
[rotationAnimation setFromValue:DegreesToNumber(0)];
[rotationAnimation setToValue:DegreesToNumber(360)];
[rotationAnimation setDuration:2.0f];
[rotationAnimation setRepeatCount:10000]; // keep spinning
[shapeLayer addAnimation:rotationAnimation forKey:@"rotate"];
degreestonumber是一个助手函数,它将度数转换为弧度并返回nsnumber对象:
CGFloat DegreesToRadians(CGFloat degrees)
{
return degrees * M_PI / 180;
}
NSNumber* DegreesToNumber(CGFloat degrees)
{
return [NSNumber numberWithFloat:
DegreesToRadians(degrees)];
}
网上有很多关于核心动画的文章,但这应该能让你开始。如果你需要澄清,请告诉我。