Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView doesn't scroll when touching GADBannerView subview

I have a project that uses the Google AdMob Ads SDK. I'm trying to show a few ads on the homepage along with some other buttons, some of which are below the screen.

I've used a UIScrollView and added a few GADBannerViews from DFP inside as well the buttons. The ads load just fine and I can click on the ads and buttons with no problem.

The problem is when I try to scroll the scroll view. If I start touching on the ad view, the scroll view will not scroll. If I start touching anywhere else, like a button or a blank space, the scroll view scrolls properly. It seems that the ad is somehow taking control of the touch events.

I've tried all sorts of fixes such as creating a transparent UIView above the ads, which didn't work because the taps would not register.

I've tried looping through the subviews of the GADBannerView but all the subviews' classes seem proprietary to AdMob or inaccessible. (GADWebView, _UIWebViewScrollView)

I even tried adding the ad to a UITableView to see if it would scroll there, but it did not work either.

My view controller class is quite large so if you need me to post some code, I can create a sample app to demonstrate the problem. A workaround for now is to create UIWebViews with the HTML ad code inside instead of using the GADBannerView. I've tested this and it works, but I really don't want to lose the functionality of the native method.

Is there any way to scroll a UIScrollView if you start touching on the GADBannerView and allow the ad to remain clickable?

Thanks!

like image 249
justin_713 Avatar asked Feb 28 '12 01:02

justin_713


5 Answers

This issue can be resolved by subclassing UIScrollView, conforming to the the UIGestureRecognizerDelegate protocol, and returning YES from

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

You shouldn't need to set the delegate; UIScrollView is already set as the delegate of it's gesture recognizers by default.

like image 94
Jacob Jennings Avatar answered Nov 19 '22 06:11

Jacob Jennings


The problem is a conflict between the gesture recognizer in the UIWebView used by GADBannerView and a custom recognizer in GADBAnnerView. Without subclassing UIScrollView and changing the gesture recognizer delegate, you can remove this gesture recognizer and set your object as delegate for custom recognizer with this:

- (void)preventBannerCaptureTouch:(GADBannerView*)bannerView
{
    for (UIWebView *webView in bannerView.subviews) {
        if ([webView isKindOfClass:[UIWebView class]]) {

            for (UIGestureRecognizer *gestureRecognizer in webView.gestureRecognizers) {
                if ([gestureRecognizer isKindOfClass:NSClassFromString(@"GADImpressionTicketGestureRecognizer")]) {
                    gestureRecognizer.delegate = self;
                }
            }

            for (id view in [[[webView subviews] firstObject] subviews]) {
                if ([view isKindOfClass:NSClassFromString(@"UIWebBrowserView")]) {
                    for (UIGestureRecognizer *recognizer in [view gestureRecognizers]) {
                        if ([recognizer isKindOfClass:NSClassFromString(@"UIWebTouchEventsGestureRecognizer")]) {
                            [view removeGestureRecognizer:recognizer];
                        }
                    }
                    return;
                }
            }
        }
    }
}

Then you should implement the simultaneous gesture recognizer delegate to allow simultaneously recognise:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
like image 40
Gigisommo Avatar answered Nov 19 '22 06:11

Gigisommo


For DFP you can use a DFPSwipeableBannerView instead of a DFPBannerView. Not sure how the orignal GADBanner works tho, but this should be the same. Works in UITableView.

like image 2
Sjoerd Perfors Avatar answered Nov 19 '22 06:11

Sjoerd Perfors


I had to combine two of the answers above:

   for (UIWebView *webView in bannerView_.subviews) {
        if ([webView isKindOfClass:[UIWebView class]]) {
            adView = webView;
        }

        for (id view in [[[webView subviews] firstObject] subviews]) {
            if ([view isKindOfClass:NSClassFromString(@"UIWebBrowserView")]) {
                for (UIGestureRecognizer *recognizer in [view gestureRecognizers]) {
                    if ([recognizer isKindOfClass:NSClassFromString(@"UIWebTouchEventsGestureRecognizer")]) {
                        [view removeGestureRecognizer:recognizer];
                    }
                }
            }
        }
        webView.scrollView.scrollEnabled = NO;
        webView.scrollView.bounces = NO;

    }

Where bannerView_ is a GADBannerView

like image 2
BobbyB Avatar answered Nov 19 '22 04:11

BobbyB


I ran into this issue when trying to add a DFPBannerView as a subview of the contentView of a custom cell in a table view.

For some reason, connecting an IBOutlet defined in my custom cell class to a view in the cell in my storyboard caused the scrolling to start working. The view outlet wasn't even being used, and was completely separate from the banner view - even removing it from its superview allowed the scrolling behaviour to work. Just having an outlet defined and connected to something did the trick.

I wish I could explain why this works, but it remains an iOS mystery.

like image 2
David Bagwell Avatar answered Nov 19 '22 04:11

David Bagwell