如何制作iphone的摆动图标效果?
我想在我的应用程序中来回摆动图像,类似于按下 iPhone 图标时的摆动方式。 最好的方法是什么?
这是我第一次涉足不使用动画 GIF 的动画。 我认为这个想法是稍微来回旋转图像以产生摆动效果。 我研究过使用 CABasicAnimation 和 CAKeyframeAnimation。 CABasicAnimation 每次重复时都会产生抖动,因为它跳到起始位置并且不会插值回来。 CAKeyframeAnimation 似乎是解决方案,只是我无法让它工作。 我肯定错过了什么。 这是我使用 CAKeyframeAnimation 的代码(不起作用):
NSString *keypath = @"wobbleImage";
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keypath];
animation.duration = 1.0f;
animation.delegate = self;
animation.repeatCount = 5;
CGFloat wobbleAngle = 0.0872664626f;
NSValue *initial = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 0.0f, 1.0f)];
NSValue *middle = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
NSValue *final = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
animation.values = [NSArray arrayWithObjects:initial, middle, final, nil];
[imageView.layer addAnimation:animation forKey:keypath];
或者可能有一个我只是缺少的完全简单的解决方案。 感谢任何指点。 谢谢!
I want to wobble an image back and forth in my application similar to how the iPhone icons wobble when you press down on it. What's the best way to do that?
This is my first foray into animations that's not using an animated GIF. I think the idea is to slightly rotate the image back and forth to create the wobbling effect. I've looked at using CABasicAnimation and CAKeyframeAnimation. CABasicAnimation creates a jitter every time it repeats because it jumps to the from position and doesn't interpolate back. CAKeyframeAnimation seems like the solution except that I can't get it to work. I must be missing something. Here's my code using the CAKeyframeAnimation (which doesn't work):
NSString *keypath = @"wobbleImage";
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keypath];
animation.duration = 1.0f;
animation.delegate = self;
animation.repeatCount = 5;
CGFloat wobbleAngle = 0.0872664626f;
NSValue *initial = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 0.0f, 1.0f)];
NSValue *middle = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
NSValue *final = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
animation.values = [NSArray arrayWithObjects:initial, middle, final, nil];
[imageView.layer addAnimation:animation forKey:keypath];
Or there could be a totally simpler solution that I'm just missing. Appreciate any pointers. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
简单的方法:
可能需要考虑时间和角度,但这应该可以帮助您入门。
编辑:我编辑了响应以添加代码,以便在完成后将项目恢复到原始状态。 另请注意,您可以使用
beginAnimations
上下文值将任何内容传递给开始/停止方法。 在本例中,它是摆动对象本身,因此您不必依赖特定的 ivars,并且该方法可用于任何基于 UIView 的通用对象(即文本标签、图像等)Simple way to do it:
Probably have to play with timing and angles but this should get you started.
EDIT: I edited the response to add code to put the item back in its original state when done. Also, note that you can use the
beginAnimations
context value to pass along anything to the start/stop methods. In this case it's the wobbling object itself so you don't have to rely on specific ivars and the method can be used for any generic UIView-based object (i.e. text labels, images, etc.)Ramin 的回答非常好,但是从 OS4 开始,在一个简单的函数中使用 animateWithDuration 也可以实现相同的效果。
(为未来的谷歌员工改编了他的例子)
Ramin's answer was very good, but since OS4 the same effect can be achieved using animateWithDuration in one simple function too.
(adapted his example for future googlers)
您应该使用
CAKeyframeAnimation
来制作更流畅的动画。You should use
CAKeyframeAnimation
to make a smoother animation.我编写了一个示例应用程序,尝试复制主屏幕摆动和图标移动: iPhone 示例代码:瓷砖
I have written a sample app that attempts to replicate the home screen wobble and icon movement: iPhone Sample Code: Tiles
对于最近看到这篇文章并想在 Swift 中做同样事情的人,这里是我的翻译:
For anyone who has come across this posting more recently and would like to do the same in Swift, here is my translation:
我知道的最简单的方法是使用 Core Animation。 基本上,您创建一个核心动画块,然后进行旋转变换、设置和重复计数。 然后,核心动画负责处理实现这种摆动效果所需的一切。
要启动核心动画块,只需执行以下操作:
未测试。 但也可能您还必须这样做:
AFAIK setAnimationRepeatCount 将具有动画完成、撤消、完成、撤消、完成、撤消、完成...您指定的次数的效果。 因此,您可能需要首先向左旋转,不进行重复计数,然后从此时开始进行重复计数摆动。 完成后,您可能想要旋转回恒等变换(= 不应用旋转和缩放)。
您可以通过设置动画委托来链接动画
,然后
一旦动画停止,就会调用该方法。 有关如何实现动画停止时将调用的方法,请参阅 UIView 类文档。 基本上,在该方法中,您将使用新的动画块但具有相同的上下文和动画 ID 来执行下一步(即向后旋转或其他任何操作),然后(如果需要)指定另一个 didStopSelector。
更新:
您可能想检查一下:
它会自动来回摆动。
The easiest way I know is to use Core Animation. Basically, you create an Core Animation Block, then do an rotation transform and setup and repeat count. Core Animation then takes care of everything that's needed to do this wobbling effect.
To start an Core Animation block, just do:
not tested. But it can be that you will also have to do:
A.F.A.I.K. setAnimationRepeatCount will have the effect that the animation gets done, undone, done, undone, done, undone, done... as many times as you specify. So you may want to first rotate to left with no repeat count, and then from this point start wobbling with repeat count. When done, you may want to rotate back to the identity transform (= no rotation and scaling applied).
You can chain animations by setting the animation delegate with
and then
and as soon as the animation stops, that method will be called. See the UIView class documentation for how to implement that method that will be called when the animation stops. Basically, inside that method you would perform the next step (i.e. rotating back, or anything else), with an new animation block but same context and animation ID, and then (if needed) specify another didStopSelector.
UPDATE:
You may want to check out:
this will wobble back and forth automatically.
您可以使用 CAKeyframeAnimation 创建不间断的摆动效果,如下所示:
You can create a not-endless wobble effect using the
CAKeyframeAnimation
, like so:从上面的答案,我得到了 swift5 版本:
from above answers, I got the swift5 version:
聚会迟到了。 我通常使用带阻尼的弹簧(iOS7 及更高版本)。 简而言之,它看起来像:
您可以通过调整
initialSpringVelocity
和SpringWithDamping
来调整效果Late to the party. I'm typically using spring with damping (iOS7 and newer). In swift it looks like:
You can tweak the effect by adjusting the
initialSpringVelocity
andSpringWithDamping
基于 EsbenB 的答案,但针对 Swift 3 和轮换进行了更新:
Based on EsbenB's answer, but updated for Swift 3 and for rotation:
使用 Swift 4+ 最简单的方法:
Easiest way to this with Swift 4+ :
那么 Ramin 给出的代码效果很好。但是,如果您使用选项卡栏应用程序并移至下一个选项卡项目,则再次返回上一个选项卡项,您每次都会看到视图已移至左侧。因此,最佳实践是使用 ViewWillAppear 方法。
因此,每次加载视图时,您都会在正确的位置找到动画。并且也可以使用此方法。
Well the code given by Ramin works well.But if you use tabbar application and move to next tab item then again come back to previous tab item,you will see that your view has been moved to left,every time.So the best practice is that you use ViewWillAppear method as.
so the every time view is loaded you will find you animation at the right place.And also use this method as well.
斯威夫特3
Swift 3