UIScrollView子视图中的拖动事件
如何向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了防止有人像我一样发现这个问题,我通过向滚动视图中的子视图添加手势识别器解决了这个问题。手势识别器将自行处理触摸事件。
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.
布鲁诺,一种可能性是使用手势识别器,正如斯科特提到的那样。
另一种可能性是使用您提到的 TouchesMoved: 方法。使用touchesMoved:方法需要你实现另外三个方法,touchesBegan:、touchesEnded:、touchesCancelled:。它们涵盖了手指触摸屏幕的阶段:
您需要在同一个类上使用所有四个方法,否则实现该方法的超类将获取触摸处理,并且您的代码将无法按预期运行。
问候,诺比
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:
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