0

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

1 Answers1

4

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()
     }
}
Creign
  • 134
  • 10
  • When I use layoutIfNeeded, it works, but can you help me with another question, im trying to make the button starts from a side, and grow until it hits the other side, but when i start the animation, the button goes to the middle, and grow from there!! – Vinnicius Pereira Nov 13 '19 at 02:50
  • Check this out bro: https://imgbbb.com/image/LaDQIJ I don't know why but when i start my animation the button starts it animation from the middle, and grow to the borders, and I fixed it on the right! Did you get what i mean?? I Updated my code – Vinnicius Pereira Nov 13 '19 at 03:08
  • got it.. i suggest you set the button width before button pressed. – Creign Nov 13 '19 at 03:17
  • I did this man, but when I animate, it returns a constraint conflict! – Vinnicius Pereira Nov 13 '19 at 03:20
  • BRO, it worked!, I changed the layoutIfNeeded to get the whole view where my button is, and it worked! Ty for you help! – Vinnicius Pereira Nov 13 '19 at 03:29