iPad - 根据设备方向(人像或风景)更改图像
在我的viewDidLoad
方法中,我应用了一个“加载图像”,该图像出现,直到webView
完成加载。我想指定一个不同的加载图像,取决于iPad所处的方向(纵向或横向)。iPad - 根据设备方向(人像或风景)更改图像
我发现了this page这是我基于我的代码。
我的代码如下所示。 (这全部包含在我的viewDidLoad
方法中)。这只适用于肖像模式。任何想法为什么我的景观代码不被称为?请帮忙!
//detect if iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//if in Portrait Orientation
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
nil];
}
//if in Landscape Orientation
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
|| [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
nil];
}
}
UPDATE
我知道你应该能够使用图像文件名修改器,从而使设备会自动拉正确的图像。我试图把网页浏览式的负载image.png在我的应用程序文件夹中的图像与所有正确的扩展名(从文件名去掉-Portrait~ipad
):
- WebView的负载图像〜iphone.png
- [email protected]~iphone.png
- 网页视图的负载图像 - 风景〜ipad.png
- [email protected]~ipad.png
- 网页视图的负载图像 - 肖像〜ipad.png
- [email protected]~ipad.png
,它仍然没有工作:(
我想出了自己。下面是我在任何情况下,什么工作是其他具有相同的问题:
//ViewController.h
@interface ViewController : GAITrackedViewController {
// ...other code
UIImage *loadingImage;
}
@property(nonatomic, strong) UIImageView *loadingImageView;
//ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
...bunch of other code....
// Subscribe to device orientation notifications, and set "orientation" - will return a number 1-5, with 5 being "unknown".
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
//**************** Start Add Static loading image ****************/
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
loadingImage = [UIImage imageNamed:@"webView-load-image~iphone.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"webView-load-image~iphone.png"],
nil];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if(orientation == 3 || orientation == 4) {
loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
nil];
NSLog(@"Set webView-load-image iPhone Landscape");
}
if(orientation == 5) {
NSLog(@"Set webView-load-image UNKNOWN");
}
if(orientation == 1) {
loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
nil];
NSLog(@"Set webView-load-image iPad Portrait");
}
}
[self.view addSubview:loadingImageView];
// End Add Static loading image
我回头一看,在应用前,我在工作的同时,我在做类似的事情。这是我得到它的工作...
我创建以处理方向的变化的方法...
BOOL isPortraitView = YES;
@implementation MyViewController
{
// a bunch of code...
- (void)setOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
isPortraitView = YES;
// do other stuff
}
else
{
isPortraitView = NO;
// do other stuff
}
}
// other code...
}
它接受一个UIInterfaceOrientation
值,所以你不必使用状态bar方法(如果我没有记错的话,那个方法不是最好的/最可预测的)。我还存储一个类级布尔值以供快速参考。然后我用willRotateToInterfaceOrientation
方法调用它。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setOrientation:toInterfaceOrientation];
}
这还可以处理加载过程中的方向更改。
当我在Xcode中实现这个代码时,在'isPortraitView = YES'行中出现错误“使用未声明的标识符”isPortraitView“”我需要做什么? – adamdehaven
在if语句之上添加'BOOL isPortraiView;' – Alex
我会将它添加到.h文件中,它可以在类中使用。 – CoderMarkus
高兴你理解了它,但在上帝的份,除去硬编码数值为导向!有宣告此枚举:
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
};
和
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
};
而且你应该检查你的viewController的interfaceOrientation
,而不是设备的方向。
此外,检查出下面的宏:
UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)
您的代码应该看起来更像是:
UIInterfaceOrientationIsLandscape(self.interfaceOrientation) {
// show landscape image
} else {
// show portrait image
}
我尝试了一些类似于你的代码的东西,并且因为某些原因没有工作...?所以除非你能像我的答案显示的那样执行它,否则我会被困在我的:((我是一个新手) – adamdehaven
你在加载过程中改变了iPad的方向是什么? – CoderMarkus
否。iPad处于横向模式,然后启动应用程序。 – adamdehaven