开发者

UIView Resize Animation Glitches

开发者 https://www.devze.com 2023-03-18 23:13 出处:网络
I\'ve been struggling with a seemingly minor glitch in the way my UIView subclass animates. I have the view set up like so:

I've been struggling with a seemingly minor glitch in the way my UIView subclass animates. I have the view set up like so:

self.contentMode = UIViewContentModeRedraw;
self.contentStretch = CGRectMake(0.5f, 0.5f, 0.0f, 0.0f);

The view is basically a rectangle that's got a 3-pixel stroke. Because I want the view to not distort during resize, I have the contentStretch property indicating the middle of the view. Problem is, distortion happens while it's growing, but it's smooth when shrinking.

Seems like my animation block is p开发者_JAVA技巧retty straightforward; I have separate ones for growing and shrinking. Here's the growing block:

[UIView animateWithDuration:0.5 animations:^{
    CGRect newRect = CGRectMake(20, 100, 200, 70);
    [titleView setFrame:newRect];
}];

This is pretty hard to describe in a post, so I assembled a test case project that isolates only the parts that are relevant to this. If anyone wants to take a look?

http://dl.dropbox.com/u/2798577/ViewResizeTest2.0.zip

You can see when you hit the "Grow" button, the view's left side "glitches" before animating. While clearly visible here, it's a tad worse in my real project.

I'm not sure if this is a bug in my code or a problem with the framework? Hoping it's the former...

Thanks, Aaron.

Update Having received Jacob's generous feedback, I've gone back to my app and removed the contentMode setting as UIViewContentModeRedraw, noting as he does that it will run drawRect every time it animates, and perhaps that initial frame is part of the glitch. So now the animation is smooth, but it animates to the wrong position, squishing the sides. I've updated the linked project to something that more-accurately reflects what's happening in my app.

If I had to guess, it would appear that the contentStretch property isn't doing its job. It's resizing the whole thing and not a CGRect region inside as per my instruction. Can anyone confirm if I'm using that correctly?


I've got this figured out now; here's the answer for anyone else who runs into this issue.

The documentation for UIView's contentStretch property doesn't tell the full story. In my case, I understood the purpose of contentStretch to describe a "sample" of an area that should be altered in size. Instead, contentStretch needs to describe the entire region that would be subject to resizing. That makes better sense now as I write this, but it wasn't obvious to me before.

Therefore, my original declaration:

self.contentStretch = CGRectMake(0.5f, 0.5f, 0.0f, 0.0f);

described a rect that had no width or height! So it effectively did nothing. Instead, I am using a rect that includes most of the view's bounds, except for the ends, which I didn't want distorted by the size change:

self.contentStretch = CGRectMake(0.2, 0, 0.7, 1);

Note that it's a rect that starts 20% into the x axis, and spans 70% of the width, at full height. In the sizes that my rect needed to be, this covered it beautifully.

Thanks to Jacob for his advice to remove the contentMode property; that was totally unnecessary in this circumstance.

Cheers, Aaron.


Get rid of the following line in TitleView.m:

self.contentMode = UIViewContentModeRedraw;

The reason why you don't want to use UIViewContentModeRedraw is because it redisplays the view’s content when its bounds change. Hence the jagged first frame in the animation.

The default contentMode is UIViewContentModeScaleToFill, which is what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消