Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用SpriteKit在Xcode 6中通过滑动找到角度?_Objective C_Sprite Kit_Swipe_Uipangesturerecognizer - Fatal编程技术网

Objective c 如何使用SpriteKit在Xcode 6中通过滑动找到角度?

Objective c 如何使用SpriteKit在Xcode 6中通过滑动找到角度?,objective-c,sprite-kit,swipe,uipangesturerecognizer,Objective C,Sprite Kit,Swipe,Uipangesturerecognizer,我正在打两场比赛,对于这两场比赛,我想知道如何从侧击中找到一个角度。我听说你用的是UIPanGesutre,但我不知道怎么用。有什么帮助吗?您可以检测触摸的开始和停止位置坐标,并计算与两点的角度。在触摸开始事件中,确定触摸位置: 在界面中创建一个名为initialLocation的CGPoint类型的实例变量 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //Determine touch locati

我正在打两场比赛,对于这两场比赛,我想知道如何从侧击中找到一个角度。我听说你用的是
UIPanGesutre
,但我不知道怎么用。有什么帮助吗?

您可以检测触摸的开始和停止位置坐标,并计算与两点的角度。

触摸开始
事件中,确定触摸位置:

在界面中创建一个名为
initialLocation
CGPoint
类型的实例变量

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //Determine touch location and store in instance variable (so you can use it again in touchesEnded)
    UITouch *touch = [touches anyObject];
    initialLocation = [touch locationInNode:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //Determine touch location
    UITouch *touch = [touches anyObject];
    CGPoint finalLocation = [touch locationInNode:self];

    //Find change in x and y between the touches
    CGFloat changeX = finalLocation.x - initialLocation.x;
    CGFloat changeY = finalLocation.y - initialLocation.y;

    //Use trig to find angle (note this angle is in radians)
    float radians = atan(changeY/changeX);

    //Convert to degrees
    float degrees = 360 * radians/(2 * M_PI);

    //*Note* you would have to alter this part depending on where you are measuring your angle from (I am using the standard counterclockwise from right)
    //This step is due to the limitation of the atan function
    float angle;
    if (changeX > 0 && changeY > 0){
        angle = degrees;
    }
    else if (changeX < 0 && changeY > 0){
        angle = 180 + degrees;
    }
    else if (changeX < 0 && changeY < 0){
        angle = 180 + degrees;
    }
    else{
        angle = 360 + degrees;
    }
    NSLog(@"Now: %f",angle);
    //^^^ HERE IS THE ANGLE! ^^^

}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
//确定触摸位置并存储在实例变量中(以便您可以在touchesEnded中再次使用它)
UITouch*touch=[触摸任何对象];
initialLocation=[touch locationInNode:self];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
//确定触摸位置
UITouch*touch=[触摸任何对象];
CGPoint finalLocation=[touch locationInNode:self];
//查找触摸之间x和y的变化
CGFloat changeX=finalLocation.x—initialLocation.x;
CGFloat changeY=finalLocation.y—initialLocation.y;
//使用trig查找角度(注意此角度以弧度为单位)
浮动弧度=atan(变化Y/X);
//换算成度
浮动角度=360*弧度/(2*M_-PI);
//*注*您必须根据测量角度的位置更改此部分(我使用的是从右侧逆时针方向的标准)
//此步骤是由于atan功能的限制
浮动角;
如果(changeX>0&&changeY>0){
角度=度;
}
else if(changeX<0&&changeY>0){
角度=180度以上;
}
else if(changeX<0&&changeY<0){
角度=180度以上;
}
否则{
角度=360度以上;
}
NSLog(@“现在:%f”,角度);
//^^^这是角度^^^
}

我希望这有帮助

“我该如何计算角度呢?”Young_程序员——这有点奏效。当我向右滑动时,有时显示360度,有时显示约0。实际上,当我在上半部分滑动时,它打印0,但下半部分打印约360年,这是有意义的。如果您不熟悉微积分,此图将解释微积分中的角度: