我是Swift的新人。我想检测设备是否正在旋转(纵向或横向)。当然,我需要在Swift中使用它。 Objective C代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
-(void) detectOrientation {
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
[self doLandscapeThings];
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
[self doPortraitThings];
}
}
谢谢。
答案 0 :(得分:4)
要检测旋转,您可以实现以下两种方法:
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
}
将这些代码翻译成swift是你必须自己尝试的东西,对swift的基本知识不应该是一个问题。如果你有特定的问题,你可以随时在这里发布,但我们不能为你做功课: - )
(提示:这是你代码的一行:
if UIDevice.currentDevice().orientation == .LandscapeLeft {
}
)