Smooth expanding UIView animation in Swift

11,309

Solution 1

You can try this, I set the duration to 5 seconds but you got the idea.

let viewWidth = view.frame.width
let viewHeight = view.frame.height
UIView.animate(withDuration: 5) { 
    subview.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
}

Solution 2

It looks like you need to play with scale.

class ViewController: UIViewController {

var subview:UIView!

@IBOutlet weak var animateButton:UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    subview = UIView()
    subview.backgroundColor = .red
    subview.frame = CGRect(x: 60, y: 200, width: 50, height: 50)
    self.view.addSubview(subview)

    self.view.bringSubview(toFront: animateButton)
}

@IBAction func animateButtonPressed(sender:UIButton) {
    if(sender.tag == 0) {

        let screenCenter = CGPoint(x:UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
        let subviewCenter = self.view.convert(self.subview.center, to: self.view)
        let offset = UIOffset(horizontal: screenCenter.x-subviewCenter.x, vertical: screenCenter.y-subviewCenter.y)

        let widthScale = UIScreen.main.bounds.size.width/subview.frame.size.width
        let heightScale = UIScreen.main.bounds.size.height/subview.frame.size.height
        UIView.animate(withDuration: 1.0, animations: {
            let scaleTransform = CGAffineTransform(scaleX: widthScale, y: heightScale)
            let translateTransform = CGAffineTransform(translationX: offset.horizontal, y: offset.vertical)
            self.subview.transform = scaleTransform.concatenating(translateTransform)
        }, completion: { (finished) in
            sender.tag = 1;
        })

    } else {
        UIView.animate(withDuration: 1.0, animations: {
            self.subview.transform = CGAffineTransform.identity
        }, completion: { (finished) in
            sender.tag = 0;
        })
    }
}
}

enter image description here

Share:
11,309
user4806509
Author by

user4806509

Today I jump from Visual Basic 6 to the new world of Swift 1.1 Xcode 6.1. This week I jumped into Swift 2.0 Xcode 7.0 after a very brief tour of Swift 1.2 Xcode 6.4. Now to keep this profile updated. I'm mainly on Xcode 7.3.1 with Swift 2.3, creating and maintaining iOS 7 onwards. I'm also on Xcode 8 as well. Now I'm on Xcode 9, actually 7 through to 9, and becoming more nimble each day.

Updated on June 04, 2022

Comments

  • user4806509
    user4806509 almost 2 years

    Aim:

    How can I animate a UIView that I want to expand to fill the whole screen. The UIView needs to expand in smooth, even and balance way while it animates.

    I have one red square positioned on screen, starts small then expands to fit the screen. (Image 1.)

    let subview = UIView()
    subview.backgroundColor = .red
    subview.frame = CGRect(x: 60, y: 200, width: 50, height: 50)
    self.view.addSubview(subview)
    

    Question:

    In Swift 2 and Swift 3, using an animateWithDuration, how do I animate the the red square UIView expanding in a balanced and even manner in all directions to fill the whole screen?

    UIView.animateWithDuration(1.0, delay: 0, options: nil, animations: {
    
        ?????
    
    }, completion: nil)
    

    Image 1:

    enter image description here

  • user4806509
    user4806509 almost 7 years
    Thank you for this suggestion.
  • user4806509
    user4806509 almost 7 years
    This does it quite simply. Thank you.