UIScrollView doesn't scroll when touching GADB

2019-03-16 04:12发布

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!

8条回答
Animai°情兽
2楼-- · 2019-03-16 04:36

I ran into the same issue, but decided on a simpler solution. I found that just disabling the ad's web view bounce back allowed the parent scrollview to scroll properly when the ad was touched. Since the ad is the same size as the webview, the only thing the gestureRecognizer was doing was showing the bounce back behavior. Just had to turn that off and left the current gestureRecognizer in place.

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

            webView.scrollView.bounces = NO;     
        }
    }
}
查看更多
趁早两清
3楼-- · 2019-03-16 04:38

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.

查看更多
SAY GOODBYE
4楼-- · 2019-03-16 04:39

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;
}
查看更多
在下西门庆
5楼-- · 2019-03-16 04:43

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

查看更多
一夜七次
6楼-- · 2019-03-16 04:44

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.

查看更多
forever°为你锁心
7楼-- · 2019-03-16 04:49

Unfortunately there is not a not a way to override the scrolling gesture but retain the touch gesture for the ad. The GADBannerView itself needs to control all of the gestures on itself. There is also no way to programmatically send a click to the GADBannerView either, so you can't override the touch behavior either.

I would recommend using ads that are much smaller than your UIScrollView, so you don't have to worry too much about scrolling over an ad.

查看更多
登录 后发表回答