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

Iphone 如何在UIView中处理背景图像方向,iphone,objective-c,ios,background-image,screen-orientation,Iphone,Objective C,Ios,Background Image,Screen Orientation,我正在使用下面的方法设置背景图像。当我旋转设备时,背景会重复,这很有意义,因为它不是图像。如果这是我设置背景图像的方式,我如何处理方向变化 - (void)viewDidLoad { [super viewDidLoad]; UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; self.view.backgroundColo

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

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

如果我理解正确,每次更改方向正确时,都会创建或覆盖背景。默认情况下,
backgroundColor
nil
。您可以对此进行检查,如果为零,则继续设置值。 就像

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

如果我理解正确,每次更改方向正确时,都会创建或覆盖背景。默认情况下,
backgroundColor
nil
。您可以对此进行检查,如果为零,则继续设置值。 就像

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

您必须为水平和垂直方向拍摄2张图像,您可以使用
[…:colorWithPatternImage:…]并在方向更改为视图背景时设置它


快乐的图像编码…

您必须为水平和垂直方向拍摄两张图像,您可以使用
[…:colorWithPatternImage:…]代替分配并在方向更改为视图背景时设置它


快乐图标…

我花了一段时间才理解这个概念。我不想创造同样的肖像和风景。这里的关键是,
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;
}

我花了一段时间才理解这个概念。我不想创造同样的肖像和风景。这里的关键是,
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;
}

我希望避免制作相同的图像。使用UIImageView是正确的方法吗?有没有办法保持背景图像静止,只旋转我的UI的其余部分?是的,尝试使用imageview。它会起作用的。但是你需要根据视图设置框架。。希望你明白我想说的…我要求更好的答案。当设备倾斜时,我不想旋转背景图像,只想旋转上面的其他视图…好的,我只是研究了一个很好的解决方案。“按视图设置框架”是什么意思?你能详细说明一下使用代码吗?我想避免使用相同的图像。使用UIImageView是正确的方法吗?有没有办法保持背景图像静止,只旋转我的UI的其余部分?是的,尝试使用imageview。它会起作用的。但是你需要根据视图设置框架。。希望你明白我想说的…我要求更好的答案。当设备倾斜时,我不想旋转背景图像,只想旋转上面的其他视图…好的,我只是研究了一个很好的解决方案。“按视图设置框架”是什么意思?你能用代码详细说明一下吗?