目前我的應用程序看起來像這樣在人像模式: 如何在Swift中更改橫向模式下的圖標旋轉?
現在看起來,在橫向模式:
我知道如何旋轉圖標,但我不知道我必須粘貼代碼。我只想看看我的應用程序在橫向模式,如第二張圖片。
編輯
目前我的應用程序看起來像這樣在人像模式: 如何在Swift中更改橫向模式下的圖標旋轉?
現在看起來,在橫向模式:
我知道如何旋轉圖標,但我不知道我必須粘貼代碼。我只想看看我的應用程序在橫向模式,如第二張圖片。
編輯
您需要旋轉你的yourimageview
90度橫向模式 -
在AppDelegate.swift內"didFinishLaunchingWithOptions"
功能我把:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
然後AppDelegate
類中,我把下列功能:
func rotated()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
//Rotate 90 degrees clockwise:
yourimageview1.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
//Rotate 90 degrees counterclockwise:
yourimageview2.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
}
希望這有助於任何人!
你需要使用NSNotificationCenter在AppDelegate中設置觀察下面的代碼
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
didFinishedLaunching方法。
而在觀察者方法您檢查方向
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
你也可以這樣做在任何UIViewController viewDidLoad方法中添加代碼併爲其添加觀察者。 – RBN
您應該Assets
管理它,如果你deployment target
等於或大於8.0
更大。您可以爲不同尺寸的類別設置不同的圖像。例如,對於iphone中的肖像模式,您可以設置Compact width Regular Height
尺寸等級的分類和風景中的IPhone,您可以爲Any width Compact Height
尺寸等級設置資產。
嘗試如下您ViewController.m
-(void)viewDidLoad
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
}
- (void) deviceOrientationChnaged:(NSNotification *) notification
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
//OR Rotate 90 degrees counterclockwise:
yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
}
希望這會有所幫助。
編輯:
請檢查我這樣一個問題:LINK
我不知道我應該在哪裏粘貼代碼 – mafioso
我編輯的帖子看看現在 – mafioso