0

所以我有这个代码来改变我的 UIImage 视图上的图像。我在 iPad 和 iPhone 上都有一个非常奇怪的行为。

这段代码工作得很好,给了我想要的结果:

- (void)viewWillAppear:(BOOL)animated {
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }
}

但是放在另一种方法中的完全相同的代码并没有给我想要的结果!它只拍摄肖像照片并将其用于风景。至于纵向,它使用完全不同的图像!

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)
toInterfaceOrientation duration:
(NSTimeInterval)duration {

    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    else if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

当我为该行设置断点时,即使方向是横向self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];,它也会触发!

我在开发通用应用程序方面没有太多经验。我刚刚采用了 Apple 的 Master-Detail App Template 并对其进行了一些修改。这里有什么问题?提前致谢!

4

1 回答 1

1

使用toInterfaceOrientation确定 willRotateToInterfaceOrientation 方法中的方向。调用此函数时,statusBarOrientation仍然是旧的。

供您参考didRotateFromInterfaceOrientation在设备旋转更改后调用。如果您在此处查询statusBarOrientation应该是正确的。

于 2012-09-10T17:50:47.557 回答