Objective c 在iOS中使用触摸输入旋转3D对象
我正在开发一个ios应用程序,在这个应用程序中,我需要旋转一个3d矩阵(3d对象),只需用手指在其上滑动即可。我用于旋转的方法是 设置旋转矩阵(浮动角度、浮动x、浮动y、浮动z、浮动*矩阵) 但是,我不知道如何使用触摸的x和y位置值来正确旋转3D矩阵。这是我正在使用的代码 -(iAction)手持面板:(UIPangestureRecognitor*)识别器{Objective c 在iOS中使用触摸输入旋转3D对象,objective-c,xcode,3d,rotational-matrices,Objective C,Xcode,3d,Rotational Matrices,我正在开发一个ios应用程序,在这个应用程序中,我需要旋转一个3d矩阵(3d对象),只需用手指在其上滑动即可。我用于旋转的方法是 设置旋转矩阵(浮动角度、浮动x、浮动y、浮动z、浮动*矩阵) 但是,我不知道如何使用触摸的x和y位置值来正确旋转3D矩阵。这是我正在使用的代码 -(iAction)手持面板:(UIPangestureRecognitor*)识别器{ if(recognizer.state == UIGestureRecognizerStateBegan) {
if(recognizer.state == UIGestureRecognizerStateBegan)
{
panStartPoint = [recognizer locationInView:self];
previousPoint.x = 0;
previousPoint.y = 0;
currentPoint.x = panStartPoint.x;
currentPoint.y = panStartPoint.y;
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
panEndPoint = [recognizer locationInView:self];
}
else if (recognizer.state == UIGestureRecognizerStateChanged)
{
previousPoint.x = currentPoint.x;
previousPoint.y = currentPoint.y;
CGPoint instanteneousPoint = [recognizer locationInView:self];
currentPoint.x = instanteneousPoint.x;
currentPoint.y = instanteneousPoint.y;
int xdifference = currentPoint.x - previousPoint.x;
int ydifference = currentPoint.y - previousPoint.y;
if((xdifference <= rotationThreshold *-1) || xdifference >= rotationThreshold) //rotationThreshold = 3;
{
if(xdifference <= rotationThreshold *-1)
{
rotationY = -1.0;
}
else
{
rotationY = 1.0;
}
if( (ydifference >= rotationThreshold *-1) && ydifference <= rotationThreshold )
{
rotationX = 0.0;
}
}
if((ydifference <= rotationThreshold *-1) || ydifference >= rotationThreshold)
{
if((ydifference <= rotationThreshold *-1))
{
rotationX = -1.0;
}
else
{
rotationX = 1.0;
}
if( (xdifference >= rotationThreshold *-1) && xdifference <= rotationThreshold )
{
rotationY = 0.0;
}
}
if(rotationX != 0.0 || rotationY != 0.0)
{
rotationDegree += 5.0f;
if(rotationDegree >= 360.0f)
{
rotationDegree = 0.0f;
}
ShaderUtils::rotatePoseMatrix(rotationDegree, rotationX, rotationY, rotationZ, &modelViewMatrix.data[0]); //rotationZ = 0;
}}}
if(recognizer.state==UIgestureCongnizerStateStart)
{
panStartPoint=[识别器位置视图:self];
上一点x=0;
上一点y=0;
currentPoint.x=panStartPoint.x;
currentPoint.y=panStartPoint.y;
}
else if(recognizer.state==UIgestureCongnizerStateEnded)
{
panEndPoint=[识别器位置查看:自];
}
else if(recognizer.state==UIgestureCongnizerStateChanged)
{
previousPoint.x=currentPoint.x;
previousPoint.y=currentPoint.y;
CGPoint instantineouspoint=[recognizer locationInView:self];
currentPoint.x=InstantineousPoint.x;
currentPoint.y=瞬时点.y;
int xdifference=currentPoint.x—previousPoint.x;
int ydifference=currentPoint.y-previousPoint.y;
if((xdifference=rotationThreshold)//rotationThreshold=3;
{
if(xdifference=rotationThreshold*-1)和&ydifference嗯..我从未使用过UIPangestureRecognitor,所以我不知道它是如何工作的,为什么不工作,但我使用UITouch移动3d对象(我发现它更容易)
好的,这就是我所做的(我没有代码了,所以我不能给你…对不起):
您可以使用TouchsBegind(UITouchPhaseBegan
)获得第一次触摸
然后,如果触摸移动,则在x轴上旋转对象(如果触摸在y轴上移动),如果触摸在x轴上移动,则在y轴上旋转对象。以及2的任意组合(您必须自己设置灵敏度)
您还可以选择如下方式移动视图中的对象:轻触两次,然后移动手指(就像笔记本电脑的轨迹板)
要在z轴上移动它,只需使用uipinchgesturecognizer
很明显,不能使用二维曲面在所有三维空间中移动三维对象
如需更多信息,请查看
我可能解释得不太清楚……但是你得到了基本的想法我认为应该会对你有所帮助。这是一个教程,有几种方法可以使它以正确的方式进行谢谢skytz。平移手势或多或少与UITOUCH相同。正如我在代码中所描述的,我遵循的逻辑与你描述的完全相同。它也会旋转,但旋转n看起来不那么自然。在网上搜索了无数个小时后,我有了一个想法,为了用2D手势旋转3d对象,你首先必须将矩阵转换成它的原始位置,然后找到它的四元数(不管是什么),然后将它们相乘。或者类似的事情。