UIButton尺寸调整动画未设置动画

问题描述 投票:0回答:1

我正在尝试使用UIView动画使按钮的尺寸加倍,但由于某种原因它无法正常工作,尺寸正确,但没有动画。

应该调整按钮大小的功能

上面的Ierelevant代码

@objc func createButtonPressed(){
        //Get the width of the button
        let buttonWidth = rightButton.frame.size.width
        let buttonFinalWidth = (rightButton.frame.size.width) * 2
        //Removes the bottom stack with buttons
        if let stackButton = self.view.viewWithTag(50){
            stackButton.removeFromSuperview()
        }
        //Add the button back with half ares size
        bottomHolder.addSubview(rightButton)
        rightButton.setImage(nil, for: .normal)

        rightButton.topAnchor.constraint(equalTo: bottomHolder.topAnchor).isActive = true
        rightButton.trailingAnchor.constraint(equalTo: bottomHolder.trailingAnchor).isActive = true
        rightButton.bottomAnchor.constraint(equalTo: bottomHolder.bottomAnchor).isActive = true
        rightButton.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
        rightButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16 , bottom: 0, right: 0)

        UIView.animate(withDuration: 5.0) {
            self.rightButton.widthAnchor.constraint(equalToConstant: buttonFinalWidth).isActive = true
        }

        print(buttonFinalWidth)

    }

下面的Ierelevant代码

swift xcode uibutton uiviewanimation
1个回答
0
投票

尝试这个:

DispatchQueue.main.async {
     UIView.animate(withDuration: 5.0) {
           self.rightButton.widthAnchor.constraint(equalToConstant: buttonFinalWidth).isActive = true
           self.view.layoutIfNeeded()
      }
 }
© www.soinside.com 2019 - 2024. All rights reserved.