Is there a method like didRotateFromInterfaceOrientation which happens during rotation in Swift?
Asked Answered
M

3

5

I am trying to .StartAnimating an UIActivityIndicator when I rotate the device, I was trying to use an override function: didRotateFromInterfaceOrientation; however, this function is called after rotation has occurred and I would like to animate during the actual rotation.

I am looking for a method which is called at the moment of rotation. I have looked through the literature, and have found only depreciated functions that might have worked.

Are there any current methods that I could use for this?

Mudra answered 26/12, 2015 at 0:38 Comment(0)
R
11

Just use this code snippet to check the orientation changes

Swift 1.x/2.x

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

Swift 3.x

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}
Rudman answered 26/12, 2015 at 0:40 Comment(2)
This seems to begin the UIActivityIndicator like I'd like, but I seem to have lost the functionality of the didRotateFromInterfaceOrientation.Mudra
I don´t know if that is possible actually. For instant recognition of device orientation change you're just gonna have to monitor the accelerometer yourself.Rudman
D
3

Try this code ,

Swift 3.x

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
    coordinator.animate(alongsideTransition: { _ in
      // Execute before rotation
    }) { _ in
        //Execute after rotation

    }
}
Dugas answered 3/5, 2017 at 16:49 Comment(0)
M
-1

I was also able to use this:

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        self.activityIndicator.startAnimating()
    }

Essentially, it allows for views to be used during the rotation. In my case, I have my UIActivityIndicator start animating when the rotation is detected. This is then followed by this code:

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

        print("Rotation did occur.")

// my code entries.
// my code entries..
// my code entries...

        self.activityIndicator.stopAnimating()   
    }

At the end of the orientation transition, the activity indicator stops animating, and since I have ticked the Hides When Stopped option in the Attributes Inspector, it then disappears.

Mudra answered 26/12, 2015 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.