Jac*_*ies 6 iphone core-animation objective-c catransform3d ios
我使用Core Animation将视图围绕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;
Run Code Online (Sandbox Code Playgroud)

如果我理解正确的话,你想要的东西如下:

为了简化您的计算,您需要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;
Run Code Online (Sandbox Code Playgroud)
我得到的是你在上面看到的图像。
希望能帮助到你!