我想要运行不同的行动,取决于您是否触摸屏幕的左或右一半。所以,当你接触到左边的部分时,就会有一个动作,当右半部分被触摸时,则是另一个动作。
我想我可以制作一个透明的按钮图像,覆盖屏幕的一半,并在触摸时使用this code运行一个动作,但我不认为这是可行的。有没有更好的方法来检测触摸的位置?
发布于 2014-02-12 20:44:16
重写touchesEnded:withEvent或touchesBegan:withEvent:方法以完成以下操作:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
if([touch locationInView:self.view]>self.size.width/2){
[self runTouchOnRightSideAction];
}else{
[self runTouchOnLeftSideAction];
}
}
}发布于 2014-02-12 20:41:53
我想出了下面的代码,它运行得非常完美。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
// If left half of the screen is touched
if (touchLocation.x < self.size.width / 2) {
// Do whatever..
}
// If right half of the screen is touched
if (touchLocation.x > self.size.width / 2) {
// Do whatever..
}
}https://stackoverflow.com/questions/21738716
复制相似问题