ios - Swift iOS -Constraint 即使调用了 view.layoutIfNeeded() 也没有动画

标签 ios swift nslayoutconstraint uiviewanimation

我有一个圆形的 likeButton,它位于圆形的 moreButton 后面。 likeButton 固定到 moreButton 的 centerX 和 centerY。当我按下 moreButton 时,我想将 likeButton 设置为比 moreButton 高 200 点的动画。

我使用 NSLayoutConstraint 来跟踪 likeButton 的 centerY 并对其进行更改。当我调用 UIView.animate 并在它的闭包中调用 self.view.layoutIfNeeded() 时, View 没有更新。

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

我哪里出错了?

let moreButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "more"), for: .normal)
    button.tintColor = .red
    button.clipsToBounds = true
    button.addTarget(self, action: #selector(moreButtonTapped), for: .touchUpInside)
    return button
}()

let likeButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "like"), for: .normal)
    button.tintColor = .blue
    button.clipsToBounds = true
    return button
}()

var likeButtonCenterY: NSLayoutConstraint?

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    setAnchors()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    moreButton.layer.cornerRadius = moreButton.frame.size.width / 2
    likeButton.layer.cornerRadius = likeButton.frame.size.width / 2
}

@objc func moreButtonTapped(){

    likeButtonCenterY?.isActive = false
    likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
    likeButtonCenterY?.isActive = true

    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func setAnchors(){
    view.addSubview(likeButton)
    view.addSubview(moreButton)

    moreButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    moreButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 100).isActive = true

    likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: moreButton.centerYAnchor)
    likeButtonCenterY?.isActive = true

    likeButton.centerXAnchor.constraint(equalTo: moreButton.centerXAnchor).isActive = true
    likeButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    likeButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

最佳答案

试试这个

@objc func moreButtonTapped(){

likeButtonCenterY?.isActive = false
likeButtonCenterY = likeButton.centerYAnchor.constraint(equalTo: self.moreButton.centerYAnchor, constant: -200)
likeButtonCenterY?.isActive = true

UIView.animate(withDuration: 0.3) {
    self.view.layoutIfNeeded()
}
}

你忘了写 likeButtonCenterY =

关于ios - Swift iOS -Constraint 即使调用了 view.layoutIfNeeded() 也没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894042/

相关文章:

ios - 将 iOS 上的 Google map 自定义为深色

iphone - iphone:使用dispatch_async进行内存管理

iphone - 如何从 NSMutableArray iPhone 应用程序中查找特定对象?

ios - 如何在保持 1 :1 ratio? 的同时在 UIImageView 上设置最大宽度和高度

swift - 根据UIButton高度设置titleLabel

iphone - 需要适用于 iOS 的声音引擎或音频引擎

swift - 删除 NSNumberFormatter 连字符

ios - 快速通过 NSNotificationCenter 发送和接收消息?

快速获取地址信息

ios - 安全地更改 NSLayoutConstraint 常量值