UIButton pass-through tap-and-scroll gesture to UIScrollView

风格不统一 提交于 2020-06-12 08:59:11

问题


I have a horizontal paginated UIScrollView with a UIButton partially covering the scroll view.

Like this:

UIView
|
|- UIScrollView
|
|- UIButton

I want to make the UIButton to not trigger on tap-scroll-and-relase-above-the-button (I want the UIScrollView to scroll instead). I want the button to only respond to tap-and-release-without-moving.

Can this easy and quickly done? Or should I subclass the UIButton and override -touchesBegan:, etc., to manually pass the touches to the scrollView when appropriate?


回答1:


After some additional research I found a reasonable solution. The problem here is that UIScrollView and UIButton are not in the same responder-chain hierarchy: the next responder for both is the parent UIView, so they don't send events to each other by default.

The solution would be to subclass UIButton and implement the

- (UIResponder *)nextResponder

method so it returns the UIScrollView.


An alternate solution is to make the UIButton a child of the UIScrollView. However, that doesn't work great if you want to keep the button at a fixed position regardless of cell scrolling.


It's somewhat annoying that there's no simpler way of doing this. :-)




回答2:


Okay then, try this. This sets the tap recognizer on the button to wait until the tap has ended, then tests to see if the touch ended in the UIButton.

UIButton * btn;
UITapGestureRecognizer * getTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodToPerformOnTap:)];
[btn addGestureRecognizer:getTap];

-(void) methodToPerformOnTap:(UITapGestureRecognizer *)sender {
    if ([sender state] == UIGestureRecognizerStateEnded)
    {
        CGPoint point = [sender locationInView:btn];
        if ( CGRectContainsPoint(btn.bounds, point) ) {

            // Point lies inside the bounds. HANDLE BUTTON TAP HERE.

        }
    }
}


来源:https://stackoverflow.com/questions/25160069/uibutton-pass-through-tap-and-scroll-gesture-to-uiscrollview

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