Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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
Ios Sprite套件检测触摸位置_Ios_Objective C_Sprite Kit - Fatal编程技术网

Ios Sprite套件检测触摸位置

Ios Sprite套件检测触摸位置,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我想根据触摸屏幕的左半部分还是右半部分来运行不同的操作。因此,当你触摸左半部时会有一个动作,当你触摸右半部时会有另一个动作 我想我可以制作一个覆盖半个屏幕的透明按钮图像,并在触摸时用于运行动作,但我认为这不是办法。有没有更好的方法来检测触摸的位置?我想出了以下代码,它工作得非常完美 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject];

我想根据触摸屏幕的左半部分还是右半部分来运行不同的操作。因此,当你触摸左半部时会有一个动作,当你触摸右半部时会有另一个动作


我想我可以制作一个覆盖半个屏幕的透明按钮图像,并在触摸时用于运行动作,但我认为这不是办法。有没有更好的方法来检测触摸的位置?

我想出了以下代码,它工作得非常完美

-(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..
    }
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
CGPoint touchLocation=[触摸位置Innode:self];
//如果触摸屏幕的左半部分
if(touchLocation.xself.size.width/2){
//做任何事。。
}
}

覆盖
touchesend:withEvent
touchestart: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];
        }
    } 
}

您可以使用手势识别器而不是按钮。然后获取触摸事件的位置并确定该点在屏幕上的位置。谢谢你的帮助,但我想我想出了一个很好的解决办法。(谢谢),但我刚刚找到解决方案并在几分钟前发布了。@ USS2255263,抱歉,当你发布你的答案时,我正在写我的答案。没问题,你的版本更干净!谢谢