【问题标题】:Touch button without removing finger from screen无需从屏幕上移开手指即可触摸按钮
【发布时间】:2012-11-12 12:33:20
【问题描述】:

有没有一种方法可以检测 UIButton 内部的触摸而不需要用户从屏幕上移开手指?

示例: 如果您有两个按钮,并且用户点击了左侧的一个,然后将手指拖到右侧,则应用程序必须识别出您正在点击右侧的按钮。

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

    您应该能够使用已经存在的按钮事件来执行此操作。例如“Touch Drag Outside”、“Touch Up Outside”、“Touch Drag Exit”等

    只需注册这些活动,看看哪些活动适合您的需求。

    【讨论】:

    • 是的,这比我的要好,但本质上是一样的。我不知道 Touch Drag Inside 和 Touch Drag Outside 等等...
    【解决方案2】:

    我会使用 UIViewController 自己实现。

    而不是使用按钮。

    在屏幕上放置两个视图(每个按钮一个)您可以制作这些按钮、imageViews 或只是 UIViews,但要确保它们有 userInteractionEnabled = NO;

    然后在 UIViewController 中使用方法touchesBegantouchesMoved

    我会在 viewController 中保存一些状态,比如...

    BOOL trackTouch;
    UIView *currentView;
    

    那么如果 touchesBegan 在您的某个视图中...

    -(void)touchesBegan... (can't remember the full name)
    {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        if (CGRectContainsPoint(firstView, point)) {
            trackTouch = YES
            //deal with the initial touch...
            currentView = firstView;  (work out which view you are in and store it)
        } else if (CGRectContainsPoint(secondView, point)) {
            trackTouch = YES
            //deal with the initial touch...
            currentView = secondView;  (work out which view you are in and store it)
        }
    }
    

    然后在 touchesMoved...

    - (void)touchesMoved... (can't remember the full name)
    {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        if (CGRectContainsPoint(secondView, point) and currentView != secondView)) {
            // deal with the touch swapping into a new view.
            currentView = secondView;
        } else if (CGRectContainsPoint(firstView, point) and currentView != firstView)) {
            // deal with the touch swapping into a new view.
            currentView = firstView;
        }
    }
    

    反正就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多