当我的设备处于纵向或横向模式时,如何才能旋转按钮?我知道这样的事情:如何在设备方向快速变化时旋转按钮?
button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
但是,我必须在我的代码中调用它?
我不想在横向模式下设置我的设备,我只是想在图标处于横向模式时旋转图标。
在此先感谢!
当我的设备处于纵向或横向模式时,如何才能旋转按钮?我知道这样的事情:如何在设备方向快速变化时旋转按钮?
button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
但是,我必须在我的代码中调用它?
我不想在横向模式下设置我的设备,我只是想在图标处于横向模式时旋转图标。
在此先感谢!
您应该重写FUNC
viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
,并调用转换还有
旋转回来时
更多关于旋转不要忘记分配CGAffineTransformIdentity
:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:
的最佳方式要做到这一点是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
}
}
这个解决方案很简单,真的很容易做到。
我猜你知道如何“旋转”按钮,所以我只是告诉你如何知道设备已经旋转。
在一个视图控制器,重写willRotateToInterfaceOrientation
:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
在该方法体,可以检查的toInterfaceOrientation
值知道设备旋转到哪个方向。例如:
switch toInterfaceOrientation {
case Portrait:
// some code here...
default:
break
}
检查此链接http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change –