Niub's swift screen rotation experience terminator (OC unified idea), swiftoc

Source: Internet
Author: User

Niub's swift screen rotation experience terminator (OC unified idea), swiftoc
Niu B's swift screen rotation experience terminator (OC unified idea)

1. AppDelegate

(1) define the variable var blockRotation: Bool = false

(2) definition method

Swift code

Func application (application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow ?) -> UIInterfaceOrientationMask {
If self. blockRotation {
Return UIInterfaceOrientationMask. All
} Else {
Return UIInterfaceOrientationMask. Portrait
}
}

 

2. viewController

(1) Get Variables

Let appDelegate = UIApplication. sharedApplication (). delegate! AppDelegate

(2) modify the value of blockRotation in viewDidLoad.

Override func viewDidLoad (){

Super. viewDidLoad ()

AppDelegate. blockRotation = true

}

(3) viewWillAppear

Override func viewWillAppear (animated: Bool ){

Let value = UIInterfaceOrientation. LandscapeLeft. rawValue

UIDevice. currentDevice (). setValue (value, forKey: "orientation ")

}

(4) viewWillDisappear

Override func viewWillDisappear (animated: Bool ){

AppDelegate. blockRotation = false

Let value = UIInterfaceOrientation. Portrait. rawValue

UIDevice. currentDevice (). setValue (value, forKey: "orientation ")

}

(5) Can the landscape screen be rotated?

// Whether automatic horizontal screen is supported. You can set the value to true if the project is adjustable.

Override func shouldAutorotate ()-> Bool {

Return false

}

Experience summary:

In the above case, a portrait screen jumps to the second landscape screen.

You need a portrait screen on the UI, and then you want to see a portrait screen on the player. What should you do? The next step is to enlarge the screen:

Call the following actions for a horizontal or vertical screen.

// MARK:-landscape Screen

Func hengp (){

AppDelegate. blockRotation = true

Let value = UIInterfaceOrientation. LandscapeLeft. rawValue

UIDevice. currentDevice (). setValue (value, forKey: "orientation ")

}

// MARK:-Portrait Screen

Func shup (){

AppDelegate. blockRotation = false

Let value = UIInterfaceOrientation. Portrait. rawValue

UIDevice. currentDevice (). setValue (value, forKey: "orientation ")

}

 

// Trigger the proxy when rotation is to occur

Override func willRotateToInterfaceOrientation (toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval ){

}

// Trigger proxy after rotation is completed. Here we need to set a new layout for the necessary interface

Override func didRotateFromInterfaceOrientation (fromInterfaceOrientation: UIInterfaceOrientation ){

// Obtain the screen mode of the current mobile phone's physical status to see whether the screen is landscape screen or landscape screen.

Let interfaceOrientation = UIApplication. sharedApplication (). statusBarOrientation

If (interfaceOrientation = UIInterfaceOrientation. Portrait)

{

// Currently in portrait Mode

Print ("portrait ")

} Else {

// The current screen is in Landscape mode

Self. theWebView ?. Frame = self. view. frame

}

}

Remember: After a landscape screen, the width and height values before the portrait screen change. If you have cached the width and height values, in this case, you must obtain the width and height of the portrait screen,

Let bWidth = CGRectGetWidth (UIScreen. mainScreen (). bounds) // <screen width

Let bHeight = CGRectGetHeight (UIScreen. mainScreen (). bounds) // <screen height

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.