Objective c 目标c:从视图/图像/按钮外部开始触摸时,检测触摸开始/下降

Objective c 目标c:从视图/图像/按钮外部开始触摸时,检测触摸开始/下降,objective-c,touch,long-press,Objective C,Touch,Long Press,我有一个类似于键盘的东西(一个主视图,九个子视图)。触摸开始/向下可以在九个子视图中的任何一个子视图中开始。我检测触碰/开始,以了解用户何时将手指拖动到其他子视图(手指拖动到哪个视图),以及用户何时抬起手指/触碰/停止 (堆栈溢出不允许发布此问题。我将尝试粘贴答案部分的代码,看看是否有效) -(void)所选语音:(CGPoint)位置{ int updatedSelectedVoiceNumber; if(CGRectContainsPoint(self.voice0.frame, loca

我有一个类似于键盘的东西(一个主视图,九个子视图)。触摸开始/向下可以在九个子视图中的任何一个子视图中开始。我检测触碰/开始,以了解用户何时将手指拖动到其他子视图(手指拖动到哪个视图),以及用户何时抬起手指/触碰/停止

(堆栈溢出不允许发布此问题。我将尝试粘贴答案部分的代码,看看是否有效)

-(void)所选语音:(CGPoint)位置{

int updatedSelectedVoiceNumber;

if(CGRectContainsPoint(self.voice0.frame, location)){
    updatedSelectedVoiceNumber=0;
    self.statusLabelOutlet.text = @"...";
}else if(CGRectContainsPoint(self.voice1.frame, location)){
    updatedSelectedVoiceNumber=1;
    self.statusLabelOutlet.text = @"...";
}else if(CGRectContainsPoint(self.voice2.frame, location)){
    updatedSelectedVoiceNumber=2;
    self.statusLabelOutlet.text = @"...";
} ...
。。。
}

我在许多stackoverflow回复中找到了部分答案。我粘贴我解决这个问题的方法(a)是为了帮助下一个人,(b)是为了寻求更好的解决方案

// in IB set long press gesture min duration to 0.1
-(IBAction)handleLongPressOnVoices:(UILongPressGestureRecognizer *)gestureRecognizer
    {
        CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];

        if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
            [self whichVoiceSelected:location];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateChanged){
            [self whichVoiceSelected:location];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateEnded){
            [self.avPlayer stop];
        }
    }

-(void)whichVoiceSelected:(CGPoint)location {

    int updatedSelectedVoiceNumber;

    if(CGRectContainsPoint(self.voice0.frame, location)){
        updatedSelectedVoiceNumber=0;
        self.statusLabelOutlet.text = @"...";
    }else if(CGRectContainsPoint(self.voice1.frame, location)){
        updatedSelectedVoiceNumber=1;
        self.statusLabelOutlet.text = @"...";
    }else if(CGRectContainsPoint(self.voice2.frame, location)){
        updatedSelectedVoiceNumber=2;
        self.statusLabelOutlet.text = @"...";
    } ...
...
}
// in IB set long press gesture min duration to 0.1
-(IBAction)handleLongPressOnVoices:(UILongPressGestureRecognizer *)gestureRecognizer
    {
        CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];

        if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
            [self whichVoiceSelected:location];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateChanged){
            [self whichVoiceSelected:location];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateEnded){
            [self.avPlayer stop];
        }
    }

-(void)whichVoiceSelected:(CGPoint)location {

    int updatedSelectedVoiceNumber;

    if(CGRectContainsPoint(self.voice0.frame, location)){
        updatedSelectedVoiceNumber=0;
        self.statusLabelOutlet.text = @"...";
    }else if(CGRectContainsPoint(self.voice1.frame, location)){
        updatedSelectedVoiceNumber=1;
        self.statusLabelOutlet.text = @"...";
    }else if(CGRectContainsPoint(self.voice2.frame, location)){
        updatedSelectedVoiceNumber=2;
        self.statusLabelOutlet.text = @"...";
    } ...
...
}