我正在制作两款游戏,对于这两款游戏,我想知道如何通过滑动找到角度。我听说你使用
UIPanGesutre
,但我不知道如何使用它。有什么帮助吗?
您可以检测触摸的起始和停止位置坐标,并计算出两点之间的角度。
在
touchesBegan
事件中,确定触摸位置:
在界面中创建一个名为
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! ^^^
}
我希望这有帮助!