如何自动旋转您自己的自定义视图?

发布于 2024-09-18 00:57:25 字数 299 浏览 3 评论 0原文

我了解到获得优雅旋转的最佳方法是在您想要调整大小或移动的视图上设置自动旋转蒙版。如果您使用像 UILabel 这样的 SDK 视图,这可以正常工作,但是如果您有自己的自定义视图,并且使用了 drawRect 方法,则它不会优雅地旋转。事实上,唯一发生的事情是它拉伸了你在drawRect中绘制的任何东西。

我尝试过在旋转之前和之后重新绘制,但它并没有给我带来平滑的旋转。

我以慢动作查看了 UITextField 自动旋转(灵活宽度),它在旋转过程中完美地跟随边缘。这就是我希望我的视图做到的,那么我该怎么做呢?我的视图在旋转之前或之后跳到正确的位置。

I've learned that the best way to get graceful rotation is to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-09-25 00:57:25

下面的行将使您的 UIView 仅拉伸中间像素。如果这不是您想要的行为,我建议您阅读 contentStretch 的文档以了解如何操作 CGRect 的值。

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

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)];
情深缘浅 2024-09-25 00:57:25

我猜想您正在查看的 UITextField 至少有三个子视图,一个显示字段边框的左端,一个显示右端,一个显示中间,自动调整大小蒙版为分别是“灵活的右边距”、“灵活的左边距”和“灵活的宽度”。如果您设置类似的自定义视图,并确保其 autoresizesSubviews 属性设置为 YES,那么您应该获得与文本字段相同的平滑调整大小。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文