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

Posted

技术标签:

【中文标题】目标c:从视图/图像/按钮外部开始触摸时检测触摸开始/向下【英文标题】:objective c: detect touch began/down when touch started from outside of view/image/button 【发布时间】:2014-11-11 06:51:51 【问题描述】:

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

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

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

我在许多 *** 回复中找到了部分答案。我正在粘贴我解决此问题的方法(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 = @"...";
     ...
...

【讨论】:

以上是关于目标c:从视图/图像/按钮外部开始触摸时检测触摸开始/向下的主要内容,如果未能解决你的问题,请参考以下文章

如何检测iCarouselButtonDemo的触摸点?

目标 C:检测 UIScrollView 上的触摸事件

长按和触摸开始

SpriteKit:如何检测从节点外部开始的节点上的触摸

使用 UIButton 进行触摸检测

在 ios 的滚动视图内检测按钮的触摸