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

doc*_*ang 1 iphone objective-c background-image screen-orientation ios

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

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

doc*_*ang 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;
}
Run Code Online (Sandbox Code Playgroud)