2016-07-28 9 views
0

デバイスがポートレートまたはランドスケープモードのときにボタンを回転させるにはどうすればよいですか?私はそれのようなものを知っています:早送りでデバイスの向きが変わるとボタンを回転させるにはどうすればいいですか?

button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 

しかし、どこで私のコードで呼び出す必要がありますか?
デバイスをランドスケープモードに設定したくない場合は、ランドスケープモードにするときにアイコンを回転させたいだけです。
ありがとうございます!

+0

このリンクをチェックするhttp://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change –

答えて

0

あなたは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

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 
} 
関連する問題