【问题标题】:Touch events (touchesMoved) not working on UIViews inside UIScrollView触摸事件 (touchesMoved) 在 UIScrollView 内的 UIViews 上不起作用
【发布时间】:2013-03-10 14:15:15
【问题描述】:

我在UIScrollView 中有一个UIView,而这些视图的UIViewControllers 没有接收到触摸事件。如果我将视图从滚动视图中取出,那么它可以工作。

UserInteraction 是所有视图的默认开启,但它仍然无法正常工作!

这一定是可能的,如果有人能指出我正确的方向,我将不胜感激!

非常感谢,

【问题讨论】:

  • 你为什么不使用手势识别器来做同样的事情
  • 我特别需要 touchesMoved 方法调用,哪种手势可以解决这个问题?
  • touchesMoved 在 UIScrollView 中不起作用
  • @Zeeshan 希望下面的代码对您有所帮助
  • 谢谢@ShashankKulshrestha

标签: iphone ios uiviewcontroller uiscrollview uitouch


【解决方案1】:

将以下代码添加到实现文件然后触摸移动

 @interface UIScrollView(Custom)
    @end
    @implementation UIScrollView(Custom)

   -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}
    @end

【讨论】:

  • @Vinodh。您能否也提一下,为什么必须这样做,以便新手了解正在发生的事情。我相信,当将 uiview 添加到滚动视图时,滚动视图会接收所有触摸事件,因此不会调用 touchesBegan、touchesMoved。如果我错了,请纠正我。
【解决方案2】:

您可以使用以下方法

 UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
 [panRecognizer setMinimumNumberOfTouches:1];
 [panRecognizer setMaximumNumberOfTouches:1];
 [panRecognizer setDelegate:self];
 [yourView addGestureRecognizer:panRecognizer];

并处理它

 -(void)move:(id)sender {

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged){
         // This will return you location in view
         CGPoint currentPoint =  [sender locationInView:self.view];

         // This will return you location in Scrollview
         CGPoint scrollPoint =  [sender locationInView:[[sender view] superview]];

    }
}

【讨论】:

    猜你喜欢
    • 2010-11-24
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多