Search code examples
swiftanimationuiviewscalealpha

UIView animations with scale and alpha


I need make animation to show/hide view (UIButton), so alpha changes from 0 to 1 same time as scale of view changes from 0 to 100% (showing) and reverse (hiding). Each animation I can do separately but how make it together? And It must showing/hiding correct when user randomly (and may be so fast, for example, hiding animation not finished - showing animation starts) interact. Any help will be appreciated.


Solution

  • continuing the previous comments, I try to make a simple example. so maybe the trick is in the transform "identity" which functions to restore the view to its origin, hopefully it helps

    enter image description here

    @IBAction func showButtonTapped(_ sender: Any) {
       UIView.animate(withDuration: 1.0, delay: 0.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: [.curveEaseIn], animations: {
           self.yourView.transform = .identity
           self.yourView.alpha = 1
    
       }, completion: nil)
    }
    
    @IBAction func hideButtonTapped(_ sender: Any) {
       UIView.animate(withDuration: 1.0, delay: 0.5, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: [.curveEaseOut], animations: {
           self.yourView.transform = CGAffineTransform.identity.scaledBy(x: 0.7, y: 0.7)
           self.yourView.alpha = 0
       })
    }