如何将 UIImage 从上到下缓慢过渡以将滤镜应用到图像?
我想让它看起来好像我的图像从上到下慢慢地被过滤。我正在添加两个图像视图。我处理过的图像在后台,未处理的图像在前面。我正在将未处理图像的高度设置为0。这是我的代码。
imageView.frame = CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 400)
imageView.image = processedImage
let nonProcessedImageView = UIImageView()
nonProcessedImageView.frame = CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 400)
nonProcessedImageView.image = nonProcessedImage
view.addSubview(nonProcessedImageView)
UIView.transition(with: nonProcessedImageView,
duration: 5.0,
animations: {
nonProcessedImageView.frame = CGRect(x: 0, y: 500, width: self.view.bounds.size.width, height: 0)
},
completion: {_ in
})
未处理的图像甚至不会出现在已处理的图像之上。
I want to make it appear as if my image is slowly getting filtered from top to bottom. I am adding two image views. My processed image is in the background and non-processed in the front. I am making the height of non-processed image 0. Here is my code.
imageView.frame = CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 400)
imageView.image = processedImage
let nonProcessedImageView = UIImageView()
nonProcessedImageView.frame = CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 400)
nonProcessedImageView.image = nonProcessedImage
view.addSubview(nonProcessedImageView)
UIView.transition(with: nonProcessedImageView,
duration: 5.0,
animations: {
nonProcessedImageView.frame = CGRect(x: 0, y: 500, width: self.view.bounds.size.width, height: 0)
},
completion: {_ in
})
The non processed image does not even appear on top of the processed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题似乎是在使用 UIView.Animate 函数时更改动画块中帧的 y 坐标会导致出现问题。
请参阅此答案
引用最重要的部分:
由于您只想减小高度,因此不需要对 y 坐标执行任何操作
这会给出一些结果,但正如您所看到的,动画不太好,因为当您减小高度时,图像视图的
contentMode
启动并给出所看到的效果。为了获得最佳结果,请将新的图像视图添加到 UIView 并降低 UIView 而不是 UIImageView 的高度
这应该会给你你想要的:
The issue seems to be that changing the
y
coordinate of the frame in the animation block leads to issues when using the UIView.Animate function.See this answer
To quote the most essential part:
Since you want to just reduce the height, you don't need to do anything to the
y
coordinateThis gives some results but as you can see, the animation is not so good because as you reduce the height, the image view's
contentMode
kicks in and gives the seen effect.For best results, add the new image view to a UIView and reduce the height of the UIView instead of the UIImageView
This should give you what you want:
这适用于从上到下的过渡。
This works for top to bottom transition.