0

My goal is to set off an animation when my second viewController is loaded. How do call the function once the view is loaded?

Here is the code:

UIView.animate(withDuration: 1.5, animations: {self.greyScreen.frame.origin.y = -0.39*self.screenHeight}, completion: nil)

Any advice much appreciated!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • viewDidAppear? viewDidLoad? – canister_exister Dec 20 '18 at 16:22
  • You should read the docs for Handling View-Related Notifications. https://developer.apple.com/documentation/uikit/uiviewcontroller – Dare Dec 20 '18 at 16:23
  • You tagged the question with `viewdidload`. It seems you already know the answer on where to do something when the view controller is loaded. Of course it makes no sense to start an animation when the view controller is loaded because it isn't visible yet. – rmaddy Dec 20 '18 at 16:30

2 Answers2

3

viewDidAppear() is the method of UIViewContoller Life Cycle which is called once the screen is completely visible i.e. loaded all views into the memory hierarchy. So put your this block of animation into this method. You'll get your animation effect

Rameez
  • 412
  • 1
  • 5
  • 16
  • perfect thanks this worked! Very new to xcode so wasn't even aware of viewDidAppear but makes lots of sense! – GarrisRyder Dec 20 '18 at 16:39
  • 1
    @GarrisRyder FYI - this has nothing to do with Xcode. This is about the lifecycle of the UIViewController class, not about the IDE you are using to write your app. It's important to understand the difference between the editor, the language, and the frameworks being used. – rmaddy Dec 20 '18 at 16:47
  • Oh okay fair point - sorry rmaddy will try to avoid this mistake next time. – GarrisRyder Dec 20 '18 at 16:51
0

Place that code inside viewDidAppear.

viewDidAppear is called when the view is shown on the device, so adding that code to this method will trigger the animation as soon as the view is displayed.

Joe McMahon
  • 3,266
  • 21
  • 33
Phil
  • 151
  • 9