在我的viewDidLoad
方法中,我正在应用一个“加载图片”,直到webView
完成加载。我想根据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];
}
}
更新
我知道您应该能够使用图像文件名修饰符,以便设备自动提取正确的图像。我已经尝试将webView-load-image.png放入我的app images文件夹中,其中包含所有正确的扩展名(并从文件名中删除-Portrait~ipad
):
它仍然无效:(
答案 0 :(得分:1)
我回顾了一段时间前我工作的应用程序,我正在做类似的事情。这就是我开始工作的方式......
我创建了一个处理方向变化的方法......
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
值,因此您不必使用状态栏方法(如果我没记错的话,该方法不是最佳/最可预测的)。我还存储了一个类级布尔值,以便快速参考。然后我在willRotateToInterfaceOrientation
方法上调用它。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setOrientation:toInterfaceOrientation];
}
这也可以在加载过程中处理方向更改。
答案 1 :(得分:1)
很高兴你弄明白了,但为了善良,请删除硬编码的数值以便定位!!为此宣布了枚举:
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
}
答案 2 :(得分:0)
我自己想通了。如果其他人遇到同样的问题,这对我有用:
//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