Cow B's swift screen rotation experience Terminator (OC Unified Thinking)
1, Appdelegate
(1) define 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, to horizontal screen of the Viewcontroller
(1) Get variables
Let appdelegate = Uiapplication.sharedapplication (). Delegate as! Appdelegate
(2) Modify the value of the blockrotation variable in viewdidload
override func Viewdidload () {
Super. Viewdidload ()
appdelegate.blockrotation = true
}
(3) Viewwillappear Setting page Horizontal screen
Override func Viewwillappear (Animated:bool) {
Let value = UIInterfaceOrientation.LandscapeLeft.rawValue
Uidevice.currentdevice (). SetValue (Value, Forkey: "Orientation")
}
(4) Viewwilldisappear Settings page back to vertical screen
Override func Viewwilldisappear (Animated:bool) {
Appdelegate.blockrotation = False
Let value = UIInterfaceOrientation.Portrait.rawValue
Uidevice.currentdevice (). SetValue (Value, Forkey: "Orientation")
}
(5) Whether the horizontal screen page supports rotation
Whether to support automatic horizontal screen. See item adjustable, can be set to True
Override Func Shouldautorotate (), Bool {
return False
}
Experience Summary:
The above situation is an interface vertical screen to jump to the second horizontal screen interface.
Need an interface can vertical screen, and then want to vertical screen player that suddenly a horizontal screen, how to do, the next step is to enlarge the Recruit:
Call the following action for the desired horizontal or vertical screen.
MARK:- Horizontal screen
func hengp () {
appdelegate. Blockrotation = true
let value = uiinterfaceorientation. Landscapeleft. RawValue
uidevice. Currentdevice(). SetValue(value, Forkey: "Orientation")
}
//MARK:- vertical screen
func shup () {
appdelegate. Blockrotation = false
let value = uiinterfaceorientation. Portrait. RawValue
uidevice. Currentdevice(). SetValue(value, Forkey: "Orientation")
}
The agent is triggered when a rotation is about to occur
Override func willrotatetointerfaceorientation (tointerfaceorientation: uiinterfaceorientation , Duration: nstimeinterval) {
}
The rotation is complete to trigger the proxy. We need to re-layout the necessary interface settings here
override func didrotatefrominterfaceorientation (frominterfaceorientation: Uiinterfaceorientation) {
Gets the screen mode of the current phone's physical state to see whether it is a horizontal or vertical screen.
let interfaceorientation = uiapplication. Sharedapplication(). Statusbarorientation
if(interfaceorientation = = uiinterfaceorientation. Portrait)
{
// current is in vertical screen mode
Print(" vertical screen ")
}Else{
// current is in the horizontal screen mode
self. Thewebview?. frame = self. View. Frame
}
}
Remember: After the horizontal screen, and the vertical screen before the width of the value is changed, if you have a cache to save the width of the value, in this case, after the horizontal screen to obtain the previous vertical screen of the saved width of the value, must be re-obtained,
Let bwidth = cgrectgetwidth(uiscreen. Mainscreen(). Bounds) ///< screen width
let bheight = cgrectgetheight(uiscreen. Mainscreen(). Bounds) ///< screen height
Cow B's swift screen rotation experience Terminator (OC Unified Thinking)