what method will called when we start to rotate device and after it finished

后端 未结 5 751
抹茶落季
抹茶落季 2021-02-01 16:54

i want to detect a rotation process on ipad programmatically. In this case i want to set a boolean into yes, when the rotation will begin, and set it false after the rotation di

相关标签:
5条回答
  • 2021-02-01 17:09

    In the UISplitViewController protocol, the new method for iOS8 is

    - (void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode
    

    There are four display modes:

    typedef enum UISplitViewControllerDisplayMode : NSInteger {
      UISplitViewControllerDisplayModeAutomatic,
      UISplitViewControllerDisplayModePrimaryHidden,
      UISplitViewControllerDisplayModeAllVisible,
      UISplitViewControllerDisplayModePrimaryOverlay,
    } UISplitViewControllerDisplayMode;
    

    BUT this method will NEVER return Automatic.

    0 讨论(0)
  • 2021-02-01 17:10

    From Apple Docs:

    Sent to the view controller just before the user interface begins rotating.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    

    Sent to the view controller after the user interface rotates:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    

    See more here: UIViewController Class Reference -> Responding to View Rotation Events

    ATTENTION: This is deprecated, see this post

    0 讨论(0)
  • 2021-02-01 17:13

    All the above methods(in the answer by @Nekto) are deprecated in iOS8.0 and later versions. Source: iOS Developer Library

    As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransitionToSize:withTransitionCoordinator: method. When the interface orientation changes, UIKit calls this method on the window’s root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.

    In iOS8 or later you can use the below method.

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
    {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    
            // Stuff you used to do in willRotateToInterfaceOrientation would go here.
            // If you don't need anything special, you can set this block to nil.
    
        } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    
            // Stuff you used to do in didRotateFromInterfaceOrientation would go here.
            // If not needed, set to nil.
    
        }];
    }
    
    0 讨论(0)
  • 2021-02-01 17:21

    SWIFT 5:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: { _ in
            // code at start of rotation
        }) { _ in
            // code at end of rotation
        }
    }
    
    0 讨论(0)
  • 2021-02-01 17:24

    For newcomers to this post, the methods suggested by Nekto have become deprecated in iOS 8. Apple suggests to use:

    -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    

    You can use the "size" parameter as an easy way to get whether it is transitioning to portrait or landscape.

    i.e.

    if (size.width > size.height)
    {
        // Position elements for Landscape
    }
    else
    {
        // Position elements for Portrait
    }
    

    More info is availablein the Docs.

    0 讨论(0)
提交回复
热议问题