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