如何处理UIView中的背景图像方向

我正在使用下面的方法设置背景图像。 当我旋转设备时,背景重复,这是有意义的,因为它不是图像。 如果这是我设置背景图像的方式,如何处理方向更改?

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

我花了一些时间来理解这个概念。 我不想创建相同的图像肖像和风景。 这里的关键是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; } 

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

快乐的iCODING ……

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

 if ( self.view.backgroundColor == nil){ //set the new values here }