应在swift中自动旋转指针面方向

应在swift中自动旋转指针面方向,swift,Swift,我是斯威夫特的新手。我想检测设备是否正在旋转(纵向或横向)。当然,我需要斯威夫特。 目标C代码: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrient

我是斯威夫特的新手。我想检测设备是否正在旋转(纵向或横向)。当然,我需要斯威夫特。 目标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的基本知识应该不会有问题。如果您有特定问题,您可以随时将其发布在此处,但我们无法为您完成作业:-)

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

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

}
)