Swift 3/4 如何为 UIImageView 的 contentMode 设置动画 [英] Swift 3/4 How to animate contentMode of UIImageView

查看:23
本文介绍了Swift 3/4 如何为 UIImageView 的 contentMode 设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是当我点击按钮切换 contentMode: scaleAspectFit &scaleAspectFill 使用动画.

 @IBAction func toggleContentModeAction(_ sender: UIButton) {sender.isSelected = !sender.isSelected如果 sender.isSelected {UIView.animate(withDuration: 0.3) {self.popImageView.contentMode = .scaleAspectFitself.view.layoutIfNeeded()}}别的 {UIView.animate(withDuration: 0.3) {self.popImageView.contentMode = .scaleAspectFillself.view.layoutIfNeeded()}}}

但对我的目标没有效果.请帮我解决,感谢柬埔寨.

解决方案

contentMode 本身不是可动画的属性.但是您可以使用

My goal is when I click on button to toggle 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()
        }
    }
}

but no effective to my goal. Please fix for me, thanks from Cambodia.

解决方案

The contentMode, itself, is not an animatable property. But you can animated between two different renditions of a view with transition(with:duration:options:animations:completion:), e.g.

@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)
}

That yields:

这篇关于Swift 3/4 如何为 UIImageView 的 contentMode 设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆