objective c:从视图/图像/按钮外部开始触摸时检测触摸开始/结束

时间:2014-11-11 06:51:51

标签: objective-c touch long-press

我有一些类似于键盘的东西(一个主视图,九个子视图)。触摸开始/结束可以从九个子视图中的任何一个开始。我检测到触碰/开始,知道用户何时将手指拖入其他子视图(将手指拖入其中),以及当用户抬起手指/触摸/停止时。

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

// 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 = @"...";
} ...

... }

1 个答案:

答案 0 :(得分:1)

我在许多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 = @"...";
    } ...
...
}