gpt4 book ai didi

ios - Swift 3/4 如何为 UIImageView 的 contentMode 设置动画

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:31 24 4
gpt4 key购买 nike

我的目标是当我点击按钮时切换 contentMode:scaleAspectFit 和 scaleAspectFill with animation。

    @IBAction func toggleContentModeAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
UIView.animate(withDuration: 0.3) {
self.popImageView.contentMode = .scaleAspectFit
self.view.layoutIfNeeded()
}
}else {
UIView.animate(withDuration: 0.3) {
self.popImageView.contentMode = .scaleAspectFill
self.view.layoutIfNeeded()
}
}
}

但对我的目标没有效果。请帮我修好,来自柬埔寨的感谢。

最佳答案

contentMode 本身不是一个动画属性。但是您可以使用 transition(with:duration:options:animations:completion:) 在 View 的两个不同再现之间设置动画。 ,例如

@IBAction func didTapButton(_ sender: UIButton) {
let contentMode: UIViewContentMode = imageView.contentMode == .scaleAspectFill ? .scaleAspectFit : .scaleAspectFill

UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: {
self.imageView.contentMode = contentMode
}, completion: nil)
}

产生:

enter image description here

关于ios - Swift 3/4 如何为 UIImageView 的 contentMode 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com