iphone - 如何判断一个3D旋转的物体离屏幕边缘有多远

标签 iphone ios objective-c core-animation catransform3d

我使用 Core Animation 将 View 绕 y 轴旋转了 50 度。 我希望 View 的边缘接触到屏幕的边缘。我该怎么做?

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

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;

enter image description here

最佳答案

如果我没理解错的话,您需要如下内容:

Final view

为了简化您的计算,您需要使用 CALayeranchorPointanchorPoint 是应用转换的地方,它的效果在旋转中特别明显。我们要做的是告诉 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;

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

希望对您有所帮助!

关于iphone - 如何判断一个3D旋转的物体离屏幕边缘有多远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17120286/

相关文章:

iphone - startDownloadingUbiquitousItemAtURL 本地文件路径?

ios - 移动叠加层 Google map iOS SDK

iOS NSKeyedUnarchiver 无法获得正确的值

ios - Objective c中连续的文本格式

iphone - 传递错误委托(delegate)

iphone - 以编程方式切换到横向模式,然后返回横向/纵向模式

iphone - 如何创建与iPhone/iPad App交互的Mac/PC服务器应用程序?

ios - 尝试将对象临时存储在 Singleton 类的 NSMutableArray 中以供以后在 iOS 中使用

ios - 将手势滑动到右上角、左上角等角落

ios - 如何将动画应用于 GMSMarker