【问题标题】:Prevent touch transfer to superview from UIImageView防止从 UIImageView 到 superview 的触摸传输
【发布时间】:2023-03-29 13:05:01
【问题描述】:

我有一个自定义垂直范围滑块,并使用 touchBegan、touchMoved 和 touchEnded 处理头部的移动。当我尝试滑动头部时,它会滑动一点,然后取消触摸并在 iOS 13 上开始交互式关闭过渡。我想防止触摸在滑动时转移到超级视图。我们如何才能做到这一点。

提前致谢。

【问题讨论】:

    标签: ios objective-c swift iphone


    【解决方案1】:

    尝试将另一个手势识别器用于另一个选择器的视图

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedOnBubble)];
        [self.bubbleView addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedOnMainView)];
       [self.view addGestureRecognizer:tap2];
    
    
    -(void)tappedOnMainView
    {
        NSLog(@"touched on main View");
        [self.vwToShow setHidden:NO];
    }
    -(void)tappedOnView
    {
        NSLog(@"tapped on slider");
        [self.vwToShow setHidden:YES];
    }
    

    UIView 继承自 UIResponder,基本的触摸事件由触发触摸开始事件的视图检测。您在主视图中添加的子视图也响应 touches started 方法。这是非常基本的。您还添加了一个带有点击手势识别器的选择器方法。

    如果你仍然想使用 touchBegan ,我认为你应该这样做:

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event 
    {
        if(theTouchLocation is inside your bubble)
        {
            do something with the touch
        }
        else
        {
            //send the touch to the next view in the hierarchy ( your large view )
           [super touchesBegan:touches withEvent:event];
           [[self nextResponder] touchesBegan:touches withEvent:event];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多