围绕其中心旋转 UIView 保持其大小 [英] Rotate UIView around its center keeping its size

查看:27
本文介绍了围绕其中心旋转 UIView 保持其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 UIView 旋转几个弧度,但在应用转换后,它看起来并没有保持其大小.实现这一目标的正确方法是什么?

I'm trying to rotate an UIView a few radians but after applying the transformation it doesn't look to be keeping its size. What's the proper way to achieve this?

这是我正在做的和我得到的(带箭头的蓝色框是我要旋转的视图——它应该与后面的红色框保持相同的外观):

Here's what I'm doing and what I get (Blue box with the arrow is the View I'm trying to rotate -- it should keep same aspect as red box behind):

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

double rads = DEGREES_TO_RADIANS(240);
CGAffineTransform transform = CGAffineTransformRotate(CGAffineTransformIdentity, rads);
self.arrowView.transform = transform;

谢谢!

推荐答案

您可能遇到了自动布局问题.您可能对旋转视图有限制,将其固定到超级视图的边缘.应用转换后,Autolayout 会更新视图的大小以仍然适合超级视图.

You're probably hitting a problem with Autolayout. You probably have constraints on the rotated view pinning it to the edges of the superview. When the transform is applied, Autolayout is updating the view's size to still fit within the superview.

您可以尝试不同的约束(例如,将视图的中心固定到另一个视图的中心,并将宽度和高度固定为恒定值)或关闭旋转视图的自动布局,或者,如果这些没有工作或不适合您的需要,使用自动布局下布局的容器视图,并将您的旋转视图添加到其中,而不使用自动布局.

You can experiment with different constraints (e.g. pinning the centre of the view to the centre of another view, and pinning the width and height to constant values) or turn Autolayout off for the rotated view, or, if these don't work or don't suit your needs, use a container view which is laid out under Autolayout, and add your rotating view to this, without using Autolayout.

这只能在代码中完成 - 您可以通过将 translatesAutoresizingMasksIntoConstraints 设置为 NO(启用自动布局)或 YES(自动布局关闭).如果从一个视图切换到另一个视图,则需要设置适当的自动调整大小蒙版.

This can only be done in code - you can make individual views subject to Autolayout or not by setting translatesAutoresizingMasksIntoConstraints to NO (Autolayout on) or YES (Autolayout off). You'll need to set the appropriate autoresizing masks if you switch a view from one to the other.

这篇关于围绕其中心旋转 UIView 保持其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆