如何为 UIImageView 中的图像变化添加动画效果?
我有一个带有图像的 UIImageView
。现在我有一个全新的图像(图形文件),并且想在这个 UIImageView
中显示它。如果我只是设置
myImageView.image = newImage;
新图像立即可见。不可动画。
我希望它能够很好地淡入新图像中。我想也许有比在其上创建一个新的 UIImageView 并与动画混合更好的解决方案?
I have an UIImageView
with an image. Now I have a completely new image (graphic file), and want to display that in this UIImageView
. If I just set
myImageView.image = newImage;
the new image is visible immediately. Not animatable.
I want it to nicely fade into the new image. I thought maybe there's a better solution than just creating a new UIImageView
on top of that and blending with animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
是另一种可能性
is another possibility
我不确定您是否可以使用淡入淡出效果对 UIView 进行动画处理,因为似乎所有支持的视图转换都在 UIViewAnimationTransition 枚举中定义。使用CoreAnimation可以实现淡入淡出效果。此方法的示例:
I am not sure if you can animate UIViews with fade effect as it seems all supported view transitions are defined in
UIViewAnimationTransition
enumeration. Fading effect can be achieved using CoreAnimation. Sample example for this approach:用迈克尔·斯科特的话来说,保持简单愚蠢。
以下是在 Swift 3 和 Swift 4 中执行此操作的简单方法:
In the words of Michael Scott, keep it simple stupid.
Here is a simple way to do this in Swift 3 and Swift 4:
下面是 Swift 中的一个示例,它将首先交叉溶解新图像,然后添加弹性动画:
如果
imageView
作为子视图正确添加到视图中,则在 < code>selected = false 到selected = true
应该用弹性动画交换图像。SNStockCellSelectionAccessoryViewImage
只是根据当前选择状态返回不同的图像,如下所示:下面的 GIF 示例速度有点慢,实际动画发生得更快:
; NBSP ;
Here is one example in Swift that will first cross dissolve a new image and then add a bouncy animation:
If
imageView
is correctly added to the view as a subview, toggling betweenselected = false
toselected = true
should swap the image with a bouncy animation.SNStockCellSelectionAccessoryViewImage
just returns a different image based on the current selection state, see below:The GIF example below is a bit slowed down, the actual animation happens faster:
Swift 5 扩展:
Swift 5 extension:
代码:
示例:
有趣、更详细的解决方案:(点击切换 )
Code:
Example:
Fun, More Verbose Solution: (Toggling on a tap)
试试这个:
Try this:
使用 Swift 3
用法:
With Swift 3
Usage:
为什么不试试这个呢。
快速版本:
Why not try this.
A swift version:
Swift 4 这太棒了
Swift 4 This is just awesome
这里有几种不同的方法: UIAnimations 据我回忆,这听起来像是您的挑战。
编辑:我太懒了:)
在这篇文章中,我指的是这个方法:
但是你不是对框架进行动画处理,而是对 alpha 进行动画处理:
There are a few different approaches here: UIAnimations to my recollection it sounds like your challenge.
Edit: too lazy of me:)
In the post, I was referring to this method:
But instead of animation the frame, you animate the alpha:
弗拉基米尔的答案是完美的,但像我这样正在寻找快速解决方案的人
此解决方案适用于 swift 版本 4.2
您可以从任何地方调用此方法。它将用动画将图像更改为下一张(图像)。
对于 Swift 版本 4.0 使用以下解决方案
Vladimir's answer is perfect, but anyone like me who is looking for swift solution
This Solution is Worked for swift version 4.2
You can call this method from anywhere. It will change the image to next one(image) with animation.
For Swift Version 4.0 Use bellow solution
从 iOS 5 开始,这一点变得更加简单:
As of iOS 5 this one is far more easy:
Swift 5:
处理 CollectionView 时。仅重新加载 1 个项目或多个带有动画的项目。我在更改单元格图像时尝试了一些不同的动画选项,没有区别,所以我在这里不指明动画名称。
Swift 5:
When deal with collectionView. Reloading only 1 item or several items with animation. I try some different animation options when changing cell image, no difference, so I don't indicate animation name here.