0

I've been learning swift for one week now and after creating my first app which uses Weather API I wanted to create simple animation: there is an image in LaunchScreen.storyboard with background and I wanted to animate the image to shrink to 0 so that my other ViewController would appear as normal. I've made something like this but there is problem, after the animation is finished Main ViewController appears and there is no animation inside of it. Moreover I wanted this image to increase its size slightly and then shrink - maybe there is another way to do it in one UIView.animate?

afterLaunchVC.swift:

class afterLaunchVC: UIViewController {

    @IBOutlet weak var logoImg: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //increasing the size
        UIView.animate(withDuration: 0.1, animations: ({
            self.logoImg.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
        }), completion: nil)
        //shrinking the image after increasing
        UIView.animate(withDuration: 1.0, animations: ({
             self.logoImg.transform = CGAffineTransform(scaleX: 0.00001, y: 0.00001)
        }), completion:{ //performingSegue
             finished in self.performSegue(withIdentifier: "MainVC", sender: self)
      })
    }
}

As you can see as a UIView.animate completion I've set performSegue and turned off the animation of it but can't get animation in MainVC.swift (IBOutlets appear normally but without animation)

MainVC.swift:

    class MainVC: UIViewController {

        @IBOutlet weak var topLabel: UILabel!
        @IBOutlet weak var hourLabel: UILabel!
        @IBOutlet weak var remainingTimeLabel: UILabel!

        var time : TimeTrack!

        override func viewDidLoad() {
            super.viewDidLoad()

            topLabel.center.y = self.view.frame.height + 100
            hourLabel.center.y = self.view.frame.height + 100
            remainingTimeLabel.center.y = self.view.frame.height + 100


            UIView.animate(withDuration: 2.6, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
                self.topLabel.center.y = self.view.frame.height/2
            }), completion: nil)

            UIView.animate(withDuration: 2.5, delay: 0.1, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
                self.hourLabel.center.y = self.view.frame.height/2
            }), completion: nil)

            UIView.animate(withDuration: 2.4, delay: 0.2, usingSpringWithDamping: 1.0, initialSpringVelocity: 3.0, options: [], animations: ({
                self.remainingTimeLabel.center.y = self.view.frame.height/2 - 30
            }), completion: nil)

            // further code...
       }

I hope someone will explain what's wrong here because I'm new to this language

mikro098
  • 2,173
  • 2
  • 32
  • 48
  • 2
    Try to put your code inside of viewWillAppear function. – Rob Jan 09 '17 at 15:41
  • Hey. Your question is a little unclear. Could you try and explain a little better and more clear as to what your issue here is? – justColbs Jan 09 '17 at 15:43
  • Why is it uclear? I've said that the problem is that the animation which is supposed to be performed in MainVC.swift doesn't appear after transition from afterLaunchVC.swift. Another problem is increasing and then decreasing the size of the image - is it possible to make somehow in one UIView.animate. Moreover is it ok to `performSegue` in UIView.animate completion? @Rob - thanks for the answer, animation appears but there is small lag when IBOutlets animate in MainVC.swift – mikro098 Jan 09 '17 at 15:46
  • ViewDidAppear would be a better option @Rob – Scriptable Jan 09 '17 at 15:48

1 Answers1

2

I suggest you familiarize yourself with the UIViewController lifecycle.

It would be best to put these animations in viewDidAppear because of this.

When the view APPEARS, you want these things to animate, there is a difference between when a view loads and when it appears.

View did Load: When a view loads into memory

View did appear: Called after view did load (and after viewWillAppear, Right when the view appears on screen

Community
  • 1
  • 1
Jerland2
  • 1,096
  • 11
  • 28