【问题标题】:Draggable UIView on ScrollView user experience not goodScrollView 上的可拖动 UIView 用户体验不好
【发布时间】:2014-08-06 04:59:23
【问题描述】:

我有一个 UITableView,在每个单元格上,我都有一个可拖动的按钮。

可拖动按钮是 UIButton 的子类,并覆盖下面的委托函数:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

首先,这个按钮可以正常工作并且可以按预期拖动。问题是,touchesBegan 委托仅在用户真正点击按钮时才被调用。 由于此按钮位于 UITableViewCell 上,如果用户的手指在按钮上快速滑动,则无法每次都调用 touchBegan 函数,而是会滚动表格。

我曾尝试使用UIEdgeInsetsMake 优化按键触摸区域,但仍然存在这个问题。我不确定有什么好的方法可以改善这种体验。

【问题讨论】:

  • 通过实现touches...:withEvent: 方法,您有效地绕过了滚动视图和手势识别器的所有行为。如果我可以问,你为什么?我的意思是,为什么不在单元格内添加UIScrollView,让滚动视图帮助您进行拖动?

标签: ios objective-c uitableview button draggable


【解决方案1】:

尝试将 UITableView 的 @property(nonatomic) BOOL delaysContentTouches 设置为 NO

【讨论】:

    【解决方案2】:

    在种植按钮时。 设置此属性。

    myButton.layer.zPosition = 10;
    

    我认为你应该检查你的事件控制并根据你的需要正确放置它。

     UIControlEventTouchUpInside // fires when u release press
     UIControlEventTouchDown // fires at the instant you press it down.
    

    还有一些,我认为你应该相应地考虑它们。

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您可以在 tableview 上覆盖 hitTest:withEvent:。

      - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
      {
          CGRect buttonArea; //figure out rect 
          if (CGRectContainsPoint(buttonArea, point))
          {
              return button;
          }
          return self;
      }
      

      这将使触摸直接转到您的按钮,而不是先转到滚动视图。

      在此处了解更多信息:https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html

      【讨论】:

        【解决方案4】:

        您应该使用 UIPanGestureRecognizer 来检测按钮矩形内的手指触摸和移动。 您可以启用 delaysTouchesBegan 属性来禁用滚动。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-05
          • 2017-09-28
          • 2013-01-18
          • 2012-05-09
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          相关资源
          最近更新 更多