0

我想创建一个图像的动画,使图像来回摆动,就像节拍器一样。动画的目标是模拟类似“旋转器”的效果。想象一下,只有上半部分,箭头会前后移动,直到它停在目标上。在此处输入图像描述

到目前为止,我已经能够让箭头来回移动,但是动画的锚点是图像的中心。我希望那个锚点成为图像的底部中心。为了模拟节拍器,锚点不能在当前位置。我该如何进行此更改?动画代码如下:

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

1 回答 1

0

CALayer 有一个anchorPoint属性,您可以将其设置到箭头图像的底部。

Apple 有一个名为 Metronome 的 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);
于 2011-11-26T00:08:13.727 回答