我希望能够根据触摸屏幕左半部分或右半部分运行不同的操作。因此,当您触摸左半部分时执行一项操作,而当您触摸右半部分时执行另一项操作。
我想我可以制作一个透明的按钮图像,覆盖屏幕的一半,并使用这个代码在触摸时运行一个操作,但我认为这不是正确的方法。有没有更好的方法来检测触摸的位置?
我希望能够根据触摸屏幕左半部分或右半部分运行不同的操作。因此,当您触摸左半部分时执行一项操作,而当您触摸右半部分时执行另一项操作。
我想我可以制作一个透明的按钮图像,覆盖屏幕的一半,并使用这个代码在触摸时运行一个操作,但我认为这不是正确的方法。有没有更好的方法来检测触摸的位置?
重写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];
}
}
}
-(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..
}
}