如何处理UIView中的背景图像方向 [英] How to deal with background image orientation in UIView

查看:88
本文介绍了如何处理UIView中的背景图像方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using setting the background image using methodology below. When I rotate my device the background repeats, which make sense because it is not an image. How do I deal with orientation change if this is the way I'm setting my background image?

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


推荐答案

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

It took me awhile to understand this concept. I didn't want to create the same image portrait and landscape. The key here is that CGAffineTransformMakeRotation rotates from the original state of your UIImageView or any UIView for that matter. This assumes your background image has orientation to it. E.g. You want your UIImageView to stay put, while other objects behaves to normal orientation change event.

- (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;
}

这篇关于如何处理UIView中的背景图像方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆