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

查看:71
本文介绍了围绕其中心旋转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天全站免登陆