【发布时间】:2016-12-10 02:59:31
【问题描述】:
但是当我的 iPad 处于横向模式时,我希望我的图标看起来像这样:
现在它在横向模式下看起来像这样:
我知道如何旋转图标,但我不知道我必须在哪里粘贴代码。我只想在横向模式下查看我的应用,就像第二张图片一样。
编辑
【问题讨论】:
标签: ios swift orientation
但是当我的 iPad 处于横向模式时,我希望我的图标看起来像这样:
现在它在横向模式下看起来像这样:
我知道如何旋转图标,但我不知道我必须在哪里粘贴代码。我只想在横向模式下查看我的应用,就像第二张图片一样。
编辑
【问题讨论】:
标签: ios swift orientation
您需要使用 NSNotificationCenter 以下代码设置观察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
在 AppDelegate didFinishedLaunching 方法中。
然后在观察者方法中检查方向
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
【讨论】:
如果您的deployment target 等于或大于8.0,您应该从Assets 管理它。您可以为不同的尺寸等级设置不同的图像。例如,对于 iPhone 中的纵向模式,您可以将资产设置为Compact width Regular Height 大小类,对于横向模式的 iphone,您可以将资产设置为Any width Compact Height 大小类。
【讨论】:
您需要在横向模式下将 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")
}
}
希望这对其他人有帮助!
【讨论】:
在您的 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
【讨论】: