2017-02-28 75 views
0

我试图检测用户是否正在触摸SKScene屏幕的左侧或右侧。快速触摸识别不识别正在触摸的屏幕的一半

我把下面的代码放在一起,但它只输出“左”,而不管在哪里被触摸。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 



for touch in touches { 
    let location = touch.location(in: self) 

    if(location.x < self.frame.size.width/2){ 
     print("Left") 
    } 

    else if(location.x > self.frame.size.width/2){ 
     print("Right") 
    } 
} 
} 

回答

1

如果您使用SpriteKit项目的默认场景,则定位点为(0.5,0.5),将场景的原点置于中心位置。在

for touch in touches { 
    let location = touch.location(in: self) 

    if(location.x < frame.midX){ 
     print("Left") 
    } 

    else if(location.x > frame.midX){ 
     print("Right") 
    } 
} 

更多细节:如果你想测试触摸位置是左还是右,你应该改变的条件检查,(不管这将工作锚点)SKScene

+0

谢谢!!一直拉着我的头发。 –

+0

无论锚点如何,您都可以使用frame.midX使其更清洁一些。 “location.x crashoverride777