如何自动旋转您自己的自定义视图?
我了解到获得优雅旋转的最佳方法是在您想要调整大小或移动的视图上设置自动旋转蒙版。如果您使用像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的行将使您的 UIView 仅拉伸中间像素。如果这不是您想要的行为,我建议您阅读 contentStretch 的文档以了解如何操作 CGRect 的值。
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.
我猜想您正在查看的 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 itsautoresizesSubviews
property is set toYES
, then you should get the same smooth resize that the text field does.