UIImageView的节拍器式动画iOS
问题描述:
我想创建一个图像动画,使图像来回摆动,非常像节拍器。动画的目标是模拟“微调”效果。想象一下,只有上半部分,箭头会前后移动,直到它停在目标上。 UIImageView的节拍器式动画iOS
到目前为止,我已经能够让箭头来回移动,但动画的锚点是图像的中心。我希望这个锚点成为图像的底部中心。为了模拟节拍器,锚点不能在当前位置。我如何去做这个改变?动画代码如下:
#define RADIANS(degrees) ((degrees * M_PI)/180.0)
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(90));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-90));
arrow.transform = leftWobble; // starting point
[UIView beginAnimations:@"wobble" context:(__bridge void *)arrow];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:10];
[UIView setAnimationDuration:.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];
arrow.transform = rightWobble; // end here & auto-reverse
[UIView commitAnimations];
答
CALayer有一个anchorPoint属性,您可以将其设置为箭头图像的底部。
苹果有一个名为节拍器的iOS示例项目。我似乎无法再在网上找到它,所以它可能已被删除,但这里有一个代码片段:
// Set up the metronome arm.
metronomeArm = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"metronomeArm.png"]];
// Move the anchor point to the bottom middle of the metronomeArm bounds, so rotations occur around that point.
metronomeArm.layer.anchorPoint = CGPointMake(0.5, 1.0);
什么单位是'anchorPoint' in?显然他们是X和Y点值。因此,对于图像的底部中心,我会沿着'CGPointMake(arrow.bounds.size.width/2,arrow.bounds.size.height)'的方向做些什么? –
Apple样本的片段显示它与图像的尺寸有关:0.5,1.0表示水平居中,垂直居中。因此,您可以完全按照原样进行操作,因为您需要相同的定位点。 – Clafou
谢谢,不知道anchorPoint属性存在。 –