2016-07-28 41 views
0

当我的设备处于纵向或横向模式时,如何才能旋转按钮?我知道这样的事情:如何在设备方向快速变化时旋转按钮?

button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 

但是,我必须在我的代码中调用它?
我不想在横向模式下设置我的设备,我只是想在图标处于横向模式时旋转图标。
在此先感谢!

+0

检查此链接http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change –

回答

1

的最佳方式要做到这一点是viewDidLoad添加以下代码行:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil) 

,然后在功能旋转设备取向的情况下做一些这样的代码:

func rotate(){ 
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) { 
    //your code here in landscape mode 
    } 
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){ 
    //your code in portrait mode 
    } 
} 

这个解决方案很简单,真的很容易做到。

0

我猜你知道如何“旋转”按钮,所以我只是告诉你如何知道设备已经旋转。

在一个视图控制器,重写willRotateToInterfaceOrientation

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

} 

在该方法体,可以检查的toInterfaceOrientation值知道设备旋转到哪个方向。例如:

switch toInterfaceOrientation { 
    case Portrait: 
     // some code here... 
    default: 
     break 
} 
相关问题