ios – Unusual UIView.animate habits altering UIButton title colour


Query

I’m attempting to animate a UIView colour from gentle gray to darkish gray and a UIButton title colour from blue to orange.

The animation is supposed to happen over 3 seconds when the UIButton is tapped.

The UIView modifications colour over 3 seconds, however the UIButton doesn’t, and immediately modifications colour.

Thanks.


Code

import UIKit

class ViewController: UIViewController {
    
    let button = UIButton(sort: .system)
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Set view color
        self.view.backgroundColor = .lightGray

        // Set preliminary title colour
        button.body = CGRect(x: 100, y: 100, width: 200, top: 100)
        button.setTitle("Button", for: .regular)
        button.setTitleColor(.blue, for: .regular)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 50)
        view.addSubview(button)
        
        // Add faucet gesture recognizer to set off animation
        let tapGesture = UITapGestureRecognizer(goal: self, motion: #selector(buttonTapped))
        button.addGestureRecognizer(tapGesture)

    }
    
    @objc func buttonTapped() {
        
        // Animate the change of view and title colour
        UIView.animate(withDuration: 3, delay: 0, choices: [.beginFromCurrentState, .curveEaseInOut], animations: {
            self.view.backgroundColor = .darkGray
            self.button.setTitleColor(.orange, for: .regular)
        })
        
    }
    
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles