6

我使用核心动画将视图围绕 y 轴旋转了 50 度。我希望视图的边缘接触到屏幕的边缘。我怎样才能做到这一点?

我知道视图的长度和视图旋转的度数(50 度),所以起初我想我可以使用三角函数来确定视图和边缘之间的距离。但是,相机的视角受m34结构属性的影响CATransform3D。如何确定移动视图以与屏幕边缘对齐所需的距离?

CATransform3D rotation = CATransform3DIdentity;
rotation.m34  = -1.0/500.0;
rotation = CATransform3DRotate(rotation, 50.0 * M_PI / 180, 0.0, 1.0, 0.0);
view.layer.transform = rotation;
view.layer.zPosition = 200;

在此处输入图像描述

4

2 回答 2

1

如果我理解正确,您需要以下内容:

最终视图

为了简化您的计算,您需要anchorPoint使用CALayer. 这anchorPoint是应用变换的地方,它的效果在旋转中特别明显。我们要做的是告诉CALayer围绕它的一个点旋转,而不是中心(这是默认值)。

这是我的loadView

UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor = [UIColor whiteColor];

CGRect frame = CGRectMake(view.frame.size.width - 100.f,
                          view.frame.size.height / 2.f - 50.f,
                          100.f, 100.f);
UIView *red = [[UIView alloc] initWithFrame:frame];
red.backgroundColor = [UIColor redColor];
[view addSubview:red];

CATransform3D rotation = CATransform3DIdentity;
rotation.m34  = -1.0/500.0;
// Since the anchorPoint is displaced from the center, the position will move with
// it, so we have to counteract it by translating the layer half its width.
rotation = CATransform3DTranslate(rotation, 50.f, 0, 0);
rotation = CATransform3DRotate(rotation, 50.0 * M_PI / 180, 0.0, 1.0, 0.0);
// We tell the anchorPoint to be on the right side of the layer (the anchor point
// position is specified between 0.0-1.0 for both axis).
red.layer.anchorPoint = CGPointMake(1.f, 0.5f);
red.layer.transform = rotation;
red.layer.zPosition = 200;

我得到的是你在上面看到的图像。

希望能帮助到你!

于 2013-06-20T19:25:20.383 回答
0

您想将最终的转换矩阵(包括 M34 更改)应用于您要计算的距离的视图边缘的端点。这将为您提供投影空间中点的最终位置。

iOS 5 添加了 GLKit,它包含一个完整的函数库,用于进行矩阵和向量计算。例如,看看 GLKMatrix4MultiplyVector3。该函数采用一个 3 部分向量并将其乘以一个 4x4 矩阵,并返回一个生成的 3 分量向量。

您可能需要 GLKMatrix4MultiplyVector4,因为如果我没记错的话,您希望向量的分量比轴数多 1(因此对于 3D 向量,您需要 4 分量向量。)向量数学不是我的强项,所以我有当我需要使用它时查找这些东西。

于 2013-06-18T18:38:10.323 回答