问题
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];
}
回答1:
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;
}
回答2:
You have to take 2 images both for horizontal and vertical and instead of allocating you can use [...: colorWithPatternImage:...];
and set it when orientation is changed to the background of the view.
hAPPY iCODING...
回答3:
If I understand correctly, Your background gets created or overwritten every time you change the orientation right. By default backgroundColor
is nil
. You can check for this, if it is nil then you go ahead and set the values.
Its like
if ( self.view.backgroundColor == nil){
//set the new values here
}
来源:https://stackoverflow.com/questions/4592192/how-to-deal-with-background-image-orientation-in-uiview