当iPad处于横向模式时,Swift会旋转图标

时间:2016-07-26 12:44:04

标签: ios swift rotation icons

目前我的应用程序在纵向模式下看起来像这样: enter image description here

但是当我的iPad处于横向模式时,我希望我的图标看起来像这样: enter image description here

到目前为止,在横向模式下看起来像这样:

enter image description here

所以我的问题是:我怎样才能旋转图标?我不需要更多的东西,只需要移动图标

3 个答案:

答案 0 :(得分:2)

将此行代码添加到viewDidLoad方法: func rotate(){ if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft { self.takephoto.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) self.recordButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) } else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight { self.takephoto.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) self.recordButton.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) } else if UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait { self.takephoto.transform = CGAffineTransformMakeRotation(CGFloat(0)) self.recordButton.transform = CGAffineTransformMakeRotation(CGFloat(0)) } else if UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown { self.takephoto.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) self.recordButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) } }
这就是功能:

print

答案 1 :(得分:0)

从支持纵向模式开始。您可以在控制器中观察UIDeviceOrientationDidChangeNotification,例如: https://stackoverflow.com/a/12086463/4209778

然后根据您的方向,您可以使用CGAffineTransformMakeRotation旋转图标(+/-)90度。

答案 2 :(得分:0)

在您的视图控制器中覆盖viewWillTransitionToSize:withTransitionCoordinator:并使用以下代码表示您的图像:imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

EG:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    let isPortrait = size.height > size.width
    imageView.transform = CGAffineTransformMakeRotation(CGFloat(isPortrait ? 0 : M_PI_2))
}