gpt4 book ai didi

swift - swift 中的 shouldAutorotateToInterfaceOrientation

转载 作者:搜寻专家 更新时间:2023-10-31 22:23:46 25 4
gpt4 key购买 nike

我是 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];
}
}

谢谢。

最佳答案

要检测旋转,您可以实现这两种方法:

    override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {

}

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

}

将代码翻译成 swift 是您必须亲自尝试的事情,只要您具备 swift 的基本知识,这应该不是问题。如果您有具体问题,您可以随时将它们发布到 SO 上,但我们无法为您做功课:-)

(提示:这是您的一行代码:

if UIDevice.currentDevice().orientation == .LandscapeLeft {

}

)

关于swift - swift 中的 shouldAutorotateToInterfaceOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25270170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com