如何同时旋转和移动 UIView? [英] How can I rotate and move a UIView at the same time?

查看:54
本文介绍了如何同时旋转和移动 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 UIView 同时移动和旋转.

I'm trying to make a UIView move and rotate at the same time.

这是我的代码:

_viewToDrag.frame = CGRectMake(x, y, size, width);
_viewToDrag.transform = CGAffineTransformMakeRotation(-M_PI_2 * multiplier));

随着乘数增加(0.0 .. 1.0),视图超出任何逻辑.

As the multiplier increases (0.0 .. 1.0) the view stretches beyond any logic.

这篇文章似乎回答了我的问题:旋转和移动 UIImageView (CocoaTouch)

This post seems to answer my question: Rotating and Moving a UIImageView (CocoaTouch)

但是,由于我有代码阅读障碍并且有点迟钝,所以可以解释一下这转化为代码的含义:

But, since I'm code-dyslexic and a bit retarded can some explain what this translates to code:

改为更改center"属性.

Change the "center" property instead.

我会发表评论,但我的声誉不允许.

I would have made a comment but my reputation doesn't allow it.

推荐答案

始终查阅文档.苹果说,在一个旁边有感叹号图形的大盒子里,在 frame 部分:

Always consult the documentation. Apple says, in a big box with an exclamation mark graphic next to it, in the section on frame:

警告:如果转换属性不是恒等转换,则此属性的值未定义,因此应忽略.

Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

center 下面写着:

中心在其父视图的坐标系内指定并以点为单位.设置此属性会更改相应的框架属性.

The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.

所以您链接到的答案不正确.鉴于它的一句话性质,没有参考来源,可能作者尝试过一次,在他碰巧安装的iOS版本上,看起来有效,他直接跳到了一个不受支持的结论.

So the answer you link to is incorrect. Given its one-sentence nature with no reference to sources, probably the author tried it once, on the version of iOS he happened to have installed, it looked like it worked and he jumped straight to an unsupported conclusion.

另一个答案是正确的.您需要将翻译构建到您的转换中.可能是通过使用 CGAffineTransformTranslate.

The other answer is correct. You need to build the translation into your transform. Likely by throwing in a use of CGAffineTransformTranslate.

因此,要设置平移为 (1000, 0) 且旋转为 -M_PI/2 的变换:

so, to set a transform with a translation of (1000, 0) and a rotation of -M_PI/2:

_viewToDrag.transform = CGAffineTransformRotate(
    CGAffineTransformMakeTranslation(1000.0, 0.0),
    -M_PI_2);

这篇关于如何同时旋转和移动 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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