UIScrollView子视图中的拖动事件

发布于 2024-12-03 23:19:36 字数 606 浏览 3 评论 0原文

如何向 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];

}

但没有任何反应!非常感谢您的帮助。 谢谢

How can I add a drag event to a subview of a UIScrollView? The structure is the following:

-UIView
   -UIScrollView
      -UIView
      -UIView
        ...

I tried to start with the following:

-(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];

}

But nothing happens! Help would be very much appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-12-10 23:19:36

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

Just incase anyone finds this question like I did, I solved this problem by adding a gesture recognizer to the subviews in the scrollview. The gesture recognizer will handle the touch event itself.

酒浓于脸红 2024-12-10 23:19:36

布鲁诺,一种可能性是使用手势识别器,正如斯科特提到的那样。

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

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

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

问候,诺比

Bruno, one possibility is to use the gesture recognizers, as Scott mentions.

Another possibility is to use the touchesMoved: method you mention. Using the touchesMoved: method requires you to implement another three methods, touchesBegan:, touchesEnded:, touchesCancelled:. They cover the phases of the finger touching the screen:

  • First contact (touchesBegan) allows you to set up variables as needed.
  • Continuous move (touchesMoved) allows you to track the move and move your content continuously.
  • And finally removing the finger (touchesEnded) where you can finalize any changes you want to keep.
  • touchesCancelled is required for clean-up if the gesture is interrupted (by phone call, or some other controller grabbing the touch event).

You need all four methods on the same class, otherwise a superclass implementing the method will grab the touch processing and your code won't run as expected.

Regards, nobi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文