如何以编程方式更改设备方向,屏幕的旋转方向应为顺时针方向
How to change the device orientation programmatically and screen's rotation direction should be in clock wise direction
我想在点击按钮时强制旋转屏幕。我有两个按钮,一个用于 顺时针方向 旋转屏幕,另一个用于 逆时针方向 旋转屏幕。
我的代码是-
1)[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"]
对于逆时针旋转及其工作正常。
2) [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationLandscapeRight] forKey:@"orientation"]
用于顺时针旋转。此代码还逆时针方向旋转屏幕。我排除顺时针方向旋转。
请帮我解决这个问题。
iOS 中的每个方向都有一个原始值。
- 纵向 = 1
- 纵向颠倒 = 2
- 横向左侧 = 3
- 横向右=4
您可以根据这些值更改方向。
这是我在 swift
中的做法
顺时针旋转:
@IBAction func rotateClockwise(_ sender: Any) {
let orientation = UIDevice.current.orientation
print("Current state:",orientation.rawValue)
var newOrientation: Int {
switch orientation.rawValue {
case 1:
return 3
case 2:
return 4
case 3:
return 2
case 4:
return 1
default:
return 1
}
}
UIDevice.current.setValue(newOrientation, forKey: "orientation")
}
逆时针旋转:
@IBAction func rotateAntiClockwise(_ sender: Any) {
let orientation = UIDevice.current.orientation
print("Current state:",orientation.rawValue)
var newOrientation: Int {
switch orientation.rawValue {
case 1:
return 4
case 2:
return 3
case 3:
return 1
case 4:
return 2
default:
return 1
}
}
UIDevice.current.setValue(newOrientation, forKey: "orientation")
}
备注
确保 select all orientations 在目标 - 一般
如果这不起作用,请将以下代码添加到视图控制器 class
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { get { return .all } }
我想在点击按钮时强制旋转屏幕。我有两个按钮,一个用于 顺时针方向 旋转屏幕,另一个用于 逆时针方向 旋转屏幕。
我的代码是-
1)[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"]
对于逆时针旋转及其工作正常。
2) [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationLandscapeRight] forKey:@"orientation"]
用于顺时针旋转。此代码还逆时针方向旋转屏幕。我排除顺时针方向旋转。
请帮我解决这个问题。
iOS 中的每个方向都有一个原始值。
- 纵向 = 1
- 纵向颠倒 = 2
- 横向左侧 = 3
- 横向右=4
您可以根据这些值更改方向。
这是我在 swift
中的做法顺时针旋转:
@IBAction func rotateClockwise(_ sender: Any) { let orientation = UIDevice.current.orientation print("Current state:",orientation.rawValue) var newOrientation: Int { switch orientation.rawValue { case 1: return 3 case 2: return 4 case 3: return 2 case 4: return 1 default: return 1 } } UIDevice.current.setValue(newOrientation, forKey: "orientation")
}
逆时针旋转:
@IBAction func rotateAntiClockwise(_ sender: Any) { let orientation = UIDevice.current.orientation print("Current state:",orientation.rawValue) var newOrientation: Int { switch orientation.rawValue { case 1: return 4 case 2: return 3 case 3: return 1 case 4: return 2 default: return 1 } } UIDevice.current.setValue(newOrientation, forKey: "orientation") }
备注
确保 select all orientations 在目标 - 一般
如果这不起作用,请将以下代码添加到视图控制器 class
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { get { return .all } }