【问题标题】:UIView animating its own heightUIView 动画自己的高度
【发布时间】:2021-07-12 14:05:42
【问题描述】:

我在 UIViewController 中做了一个 UIView 的简单示例,所以当我点击内部视图时,它会增加和减少它的高度。效果很好,但我无法使高度过渡动画化。

这里是视图控制器,后面是视图:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let theView = MyView()
        theView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(theView)
        
        NSLayoutConstraint.activate([
            theView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 50.0),
            theView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant:0.0),
            theView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0.0),
            theView.heightAnchor.constraint(equalToConstant: 925)
            ])
    }
}

class MyView: UIView {
    fileprivate var closedChanger = NSLayoutConstraint()
    fileprivate var isOpen = false
    
    init() {
        super.init(frame: CGRect.zero)
        setupView()
    }
    
    fileprivate func setupView() {
        self.backgroundColor = .gray
        
        self.translatesAutoresizingMaskIntoConstraints = true
        closedChanger = self.heightAnchor.constraint(equalToConstant: 150.0)
        NSLayoutConstraint.activate([
            closedChanger // start off with it shorter
        ])
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:))))
    }
    
    @objc func handleTap(sender: UITapGestureRecognizer) {
        UIView.animate(withDuration: 1.0, animations: {
            self.closedChanger.constant = self.isOpen ? self.closedChanger.constant - 25 : self.closedChanger.constant + 25
            // self.layoutIfNeeded()
        })
        isOpen.toggle()
    }
}

layoutIfNeeded 不影响它。是否不允许 UIView 为自己的高度变化设置动画?

【问题讨论】:

  • 我一直都在这样做,但也许你不是这样? (1) 我使用自动布局,每个constant约束都设置为isActive = true。 (2) 我设置了我需要更改的约束数组,并激活了我希望开始的约束。 (3)我以各种方式触发约束更改,但要实际执行它,我首先(a)停用当前约束数组,然后(b)激活我想要的数组,最后(c)使用`UIView.animate。从我所看到的来看,您正在尝试将我认为的东西结合起来。我的更改会影响水平和垂直的“滑出”。也许将其分解并测试?
  • 在动画闭包外部设置约束常量,在动画闭包内部调用setNeedsLayout
  • @Paulw11 - 结果相同。高度的变化有效,但没有动画。
  • @dfd - NSLayoutConstraint.activate 自动将 isActive 设置为 true。我已经尝试过与您的建议类似的方法,但我总是得到相同的结果:高度发生变化,但没有动画。
  • 你似乎有两个高度限制;添加视图时设置的 925 之一,然后添加“打开/关闭”高度约束。您是否收到冲突的约束警告?通常,当动画失败时,是因为在动画期间有其他东西触发了另一个布局传递,导致布局立即设置。

标签: ios animation uiview


【解决方案1】:

当你说视图不能动画自己时,你是对的。问题是您需要对包含视图的更改进行动画处理,因为那是正在更改的视图层次结构。

你需要对动画块外的约束进行修改,然后在动画块内的superview上调用layoutIfNeeded

@objc func handleTap(sender: UITapGestureRecognizer) {
    self.closedChanger.constant = self.isOpen ? self.closedChanger.constant - 25 : self.closedChanger.constant + 25
    UIView.animate(withDuration: 1.0, animations: {     
        self.superview?.layoutIfNeeded()
    })
    isOpen.toggle()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2016-08-25
    • 2012-03-01
    • 2023-03-14
    相关资源
    最近更新 更多