如何在 UIView 动画期间更新图像视图
How to update image View during UIView Animation
我想在动画期间更改 imageView
中的 image
。我有一个 imageView
,我的动画使 imageView
下降。在此动画期间,我需要将新照片加载到此 imageView
中(从互联网下载后),但在我尝试这样做的任何时候,此 imageView
都会回到第一个位置。如何在没有这个问题的情况下加载新图像?
我的代码:
在viewDidLoad
中加载图片功能
override func viewDidLoad() {
super.viewDidLoad()
viewModel.downloadImage({ [weak self] image in
DispatchQueue.main.async {
self?.userImageView.image = image
}
})
}
我从viewDidAppear
开始的动画方法
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 5, delay: 0, options: [.curveEaseOut, .allowAnimatedContent, .allowUserInteraction], animations: { [unowned self] _ in
self.userProfileImageView.center.y = 300
}, completion: nil)
}
我猜测您的图像视图是使用自动布局约束在故事板中定位的。当您更改图像视图的图像时,会导致布局发生;这意味着自动布局约束接管并将图像视图定位在动画开始之前的位置。
道理很简单:如果视图的 center
(或 frame
或 bounds
)是由自动布局约束定位的,则不要尝试为该视图设置动画。您可以改为为其 约束 设置动画。
我想在动画期间更改 imageView
中的 image
。我有一个 imageView
,我的动画使 imageView
下降。在此动画期间,我需要将新照片加载到此 imageView
中(从互联网下载后),但在我尝试这样做的任何时候,此 imageView
都会回到第一个位置。如何在没有这个问题的情况下加载新图像?
我的代码:
在viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
viewModel.downloadImage({ [weak self] image in
DispatchQueue.main.async {
self?.userImageView.image = image
}
})
}
我从viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 5, delay: 0, options: [.curveEaseOut, .allowAnimatedContent, .allowUserInteraction], animations: { [unowned self] _ in
self.userProfileImageView.center.y = 300
}, completion: nil)
}
我猜测您的图像视图是使用自动布局约束在故事板中定位的。当您更改图像视图的图像时,会导致布局发生;这意味着自动布局约束接管并将图像视图定位在动画开始之前的位置。
道理很简单:如果视图的 center
(或 frame
或 bounds
)是由自动布局约束定位的,则不要尝试为该视图设置动画。您可以改为为其 约束 设置动画。