objective c: detect touch began/down when touch started from outside of view/image/button

332 views Asked by At

i have something that resembles a key pad (one master view, nine subviews). the touch began/down may start in any of the nine subviews. i detect touch down/began, to know when user drags their finger into other subviews (which views finger is dragged into), and when user lifts their finger up/touch up/stop.

(stack overflow not letting this question be posted. i'll try pasting the code from answer section, see if that works).

// 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

There are 1 answers

0
tmr On BEST ANSWER

i found parts of the answer across many stackoverflow replies. i am pasting my approach to resolving this question (a) to help the next person and (b) as invitation for better solutions.

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