2016-07-26 75 views
2

目前我的應用程序看起來像這樣在人像模式: enter image description here斯威夫特旋轉圖標時,iPad是在橫向模式

但是當我的iPad是在橫向模式我想我的圖標看起來像這樣: enter image description here

現在看起來,在橫向模式:

enter image description here

所以我的問題是:我如何才能旋轉圖標?我不需要更多的東西,只是圖標應該移動

+0

我不明白。如果您在故事板中使用尺寸類別,這甚至不會成爲問題。所有視圖和圖像都能正確旋轉。 –

+0

是啊,他們旋轉,但看起來不像那 – mafioso

+0

我編輯我的帖子,現在你看它現在看起來像 – mafioso

回答

1

這行代碼添加到viewDidLoad方法:NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil)
,這是功能:

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)) 
} 
} 
0

從支持肖像模式開始。 https://stackoverflow.com/a/12086463/4209778

然後根據你的方向,你可以使用CGAffineTransformMakeRotation通過(+/-)90度旋轉你的圖標:你可以在你的控制器一樣遵守的UIDeviceOrientationDidChangeNotification。

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)) 
}