views:

191

answers:

2

I have an iPad app with a standard UIViewController/UIView setup - all rotations are allowed. The UIView draws some tiled image as background (the tile is 256*256 pixels):

- (void)drawRect:(CGRect)rect
{
    [[UIImage imageNamed: @"Background.png"] drawAsPatternInRect: rect];
}

When I turn my iPad I can see that during the rotation the image pattern of the original orientation is scaled to fit the new orientation. Then - immediately after the animation is finished - the view redraws its background pattern with the final configuration which is unscaled. The switching from a scaled to an unscaled pattern looks a bit ugly.

Is there a way to circumvent (or hide) this strecthing of the background pattern?

+1  A: 

Why not use a pattern image as background "color"? This automatically results in a tiled pattern and avoids any stunts with drawRect(). For example:

UIImage *backgroundTile = [UIImage imageNamed: @"Background.png"];
UIColor *backgroundPattern = [[UIColor alloc] initWithPatternImage:backgroundTile];
[myView setBackgroundColor:backgroundPattern];
[backgroundPattern release];
I will try your solution, but I also figured out a solution myself: when I mark view for a redraw using 'setNeedsDisplay' within the viewController's 'willRotateToInterfaceOrientation:duration' method the view will be redrawn immediately after turning the iPad, but before the rotation animation is done. This means, that the background image is stretched at the beginning of the animations and smoothly unstreched during the the animation. This is not noticable for the human eye anymore.
Halbanonym
A: 

to put simply,

[myView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: @"Background.png"] ] ];

GopiKrishnAn