Touch events (touchesMoved) not working on UIViews inside UIScrollView

前提是你 提交于 2019-12-11 02:16:47

问题


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,


回答1:


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



回答2:


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

    }
}


来源:https://stackoverflow.com/questions/15541276/touch-events-touchesmoved-not-working-on-uiviews-inside-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!