Vinnicius Pereira
Vinnicius Pereira

Reputation: 57

UIButton resize animation is not animating

I'm trying to make that my button double its size using the UIView animation, but for some reason it is not working it the size goes right, but no animated.

The function that should animate the button resizing

Irrelevant code above

@objc func createButtonPressed(){
    //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.addTarget(self, action: #selector(anima), for: .touchUpInside)
    rightButton.topAnchor.constraint(equalTo: bottomHolder.topAnchor).isActive = true
    rightButton.trailingAnchor.constraint(equalTo: bottomHolder.trailingAnchor).isActive = true
    rightButton.bottomAnchor.constraint(equalTo: bottomHolder.bottomAnchor).isActive = true




}

@objc func anima(){
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.rightButton.leadingAnchor.constraint(equalTo: self.bottomHolder.leadingAnchor).isActive = true
        self.rightButton.layoutIfNeeded()
    }, completion: nil)
}

Irrelevant code below

Upvotes: 0

Views: 226

Answers (1)

Creign
Creign

Reputation: 134

try this one:

let buttonFinalWidth = UIScreen.main.bounds.width
DispatchQueue.main.async {
     UIView.animate(withDuration: 5.0) {
           self.rightButton.widthAnchor.constraint(equalToConstant: buttonFinalWidth).isActive = true
           self.view.layoutIfNeeded()
     }
}

Upvotes: 4

Related Questions