1

我正在使用以下方法设置背景图像。当我旋转我的设备时,背景会重复,这是有道理的,因为它不是图像。如果这是我设置背景图像的方式,我该如何处理方向变化?

- (void)viewDidLoad {
    [super viewDidLoad];
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]];
    self.view.backgroundColor = background;
    [background release];
}
4

3 回答 3

6

我花了一段时间才理解这个概念。我不想创建相同的图像纵向和横向。这里的关键是CGAffineTransformMakeRotation从你的 UIImageView 或任何 UIView 的原始状态旋转。这假设您的背景图像有方向。例如,您希望您的 UIImageView 保持原状,而其他对象的行为是正常的方向更改事件。

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

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
        backgroundImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        backgroundImage.transform = CGAffineTransformMakeRotation(-M_PI / 2);
    }
    else {
        backgroundImage.transform = CGAffineTransformMakeRotation(0.0);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //set the UIImageView to current UIView frame
    backgroundImage.frame = self.view.frame;
}
于 2011-01-06T01:47:00.330 回答
0

如果我理解正确,每次您正确更改方向时,都会创建或覆盖您的背景。默认情况下backgroundColornil. 您可以检查这一点,如果它为 nil,那么您继续设置值。就像是

if ( self.view.backgroundColor == nil){
    //set the new values here
}   
于 2011-01-04T10:00:22.463 回答
0

您必须为水平和垂直拍摄 2 张​​图像,而不是分配您可以[...: colorWithPatternImage:...];在方向更改为视图背景时使用和设置它。

快乐的编码...

于 2011-01-04T12:26:59.590 回答