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];
I'd like to create an animation of an image that makes the image swing back and forth, much like a metronome. The goal of the animation is to simulate a "spinner" like effect. Imagine this with just the top half, the arrow will move back and forth until it stops on it's target.
So far I've been able to get the arrow to move back and forth, however the anchor point of the animation is the center of the image. I'd like that anchor to be the bottom center of the image. In order to simulate a metronome, the anchor can't be where it currently is. How do I go about making this change? The animation code is below:
#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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CALayer 有一个 anchorPoint 属性,您可以将其设置为箭头图像的底部。
Apple 有一个名为 Metronome 的 iOS 示例项目可以执行此操作。我似乎无法再在网上找到它,所以也许它已被删除,但这里是其中的代码片段:
CALayer has an anchorPoint property that you can set to the bottom of your arrow image.
Apple had an iOS sample project called Metronome that does this. I can't seem to find it online anymore so perhaps it has been removed, but here is a code snippet from it: