views:

250

answers:

1

I have a UIView in the middle of a view that I am using as a game playing area (in a 2d cocoa view). This image has a background image of the same size as the view. Resizing the view I use animation to make it look smooth (and that works fine). However, when the animation starts, the background image immediately changes size, tiling or being clipped to a size that when the animation finishes, the background image is physically the same size.

I don't want this, I want the image to always fit the view, regardless of the view size.

    UIImage *bgImage = [UIImage imageNamed:@"head.png"];
... // resize the image returning another image
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:resizedImage];
[UIView beginAnimations:@"resizeView" context:nil];
[UIView setAnimationDuration:.5];
int localViewSize = ... // work out view sizes
self.view.frame = CGRectMake(... ,localViewSize,localViewSize); 
[UIView commitAnimations];

It looks very odd as it jumps to a different size, then animates to the original size.

I am guessing that maybe I would have to make a separate view underneath my main view but is that the only way?

+1  A: 

I suspect that the "pattern image" is rendered to the layer contents (effectively a texture), and the layer contents are stretched to fit. The easy way around this is to set self.contentMode = UIViewContentModeCenter or so; bear in mind that that works when your view grows but will probably display a blank border when your view is shrunk.

tc.
So the contents are resized to fit the new view then the animation happens?I will try the center content mode, although ideally I want the background image to take the whole of the view at any zoom.Thanks
Woody
If the image is big enough, just use the image in a UIImageView with the correct content mode and set clipsToBounds=YES.
tc.
I decided for the moment to do it another way, as that just didn't work well!
Woody