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.
2
Answers
This works for top to bottom transition.
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: