Touch events (touchesMoved) not working on UIViews

2019-07-14 19:06发布

I have a UIView inside a UIScrollView, and the UIViewControllers for those views are not receiving the touch events. If I take the views out of the scroll view then it works.

UserInteraction is default ON of all views but it's still not working!

This must be possible and I'd be really grateful if someone could point me in the right direction!

Many Thanks,

2条回答
叼着烟拽天下
2楼-- · 2019-07-14 19:11

add the following code to implementation file then touches moved

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

   -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}
    @end
查看更多
太酷不给撩
3楼-- · 2019-07-14 19:38

You can use the following method

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

And to handle it

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

    }
}
查看更多
登录 后发表回答