【问题标题】:Drag event in subview of UIScrollViewUIScrollView 子视图中的拖动事件
【发布时间】:2011-09-09 17:52:39
【问题描述】:

如何向 UIScrollView 的子视图添加拖动事件?结构如下:

-UIView
   -UIScrollView
      -UIView
      -UIView
        ...

我尝试从以下内容开始:

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
CGPoint location = [aTouch locationInView:self.superview.superview];
[UIView beginAnimations:@"Dragging A DraggableView" context:nil];
self.frame = CGRectMake(location.x, location.y, 
                        self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];

}

但是什么也没发生!帮助将不胜感激。 谢谢

【问题讨论】:

    标签: objective-c ios drag-and-drop


    【解决方案1】:

    万一有人像我一样发现这个问题,我通过在滚动视图的子视图中添加手势识别器来解决这个问题。手势识别器将自己处理触摸事件。

    【讨论】:

      【解决方案2】:

      Bruno,正如 Scott 提到的,一种可能性是使用手势识别器。

      另一种可能性是使用您提到的 touchesMoved: 方法。使用 touchesMoved: 方法需要你实现另外三个方法,touchesBegan:, touchesEnded:, touchesCancelled:。它们涵盖了手指触摸屏幕的各个阶段:

      • 首次接触 (touchesBegan) 允许您根据需要设置变量。
      • 连续移动 (touchesMoved) 允许您跟踪移动并连续移动您的内容。
      • 最后移除手指 (touchesEnded),您可以在此处完成您想要保留的任何更改。
      • 如果手势被中断(通过电话或其他一些控制器抓取触摸事件),则需要 touchesCancelled 进行清理。

      您需要在同一个类上使用所有四个方法,否则实现该方法的超类将获取触摸处理,您的代码将无法按预期运行。

      问候,诺比

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 2011-07-11
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多