开发者

How to auto rotate your own custom views?

开发者 https://www.devze.com 2023-01-15 17:35 出处:网络
I\'ve learned that the best way to get graceful rotation is to开发者_JAVA百科 set the auto rotation mask on the view that you want resize or move. This works fine if you\'re using SDK views like UILab

I've learned that the best way to get graceful rotation is to开发者_JAVA百科 set the auto rotation mask on the view that you want resize or move. This works fine if you're using SDK views like UILabel, but if you have your own custom view that uses the drawRect method it doesn't rotate as gracefully. In fact the only thing that happens is that it stretches whatever you drew in drawRect.

I've tried redrawing both before and after the rotation, but it doesn't give me that smooth rotation.

I looked at a UITextField auto rotating (flexible width) in slow motion and it follows the edge perfectly during the rotation. That is what I want my view to do, so how do I do that? My views jump to the right position either before or after the rotation.


The following line will make your UIView stretch the middle pixel only. If this is not your desired behavior I suggest you read the documentation for contentStretch to learn how to manipulate the values of the CGRect.

[self setContentStretch:CGRectMake(0.5, 0.5, 0.0, 0.0)];


I would guess that the UITextField you're looking at has at least three subviews, one displaying the left cap of the field's border, one displaying the right cap, and one displaying the middle, with autoresizing masks of "flexible right margin", "flexible left margin", and "flexible width", respectively. If you set up your custom view something like that, and make sure its autoresizesSubviews property is set to YES, then you should get the same smooth resize that the text field does.

0

精彩评论

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