Skip to content

Commit

Permalink
Merge pull request #10 from larsacus/creeping-ads
Browse files Browse the repository at this point in the history
Creeping ads - Fixes ads creeping up the screen on refresh. Also adds a "clipping container" to contain all of the ads in the container so that they are not seen when they do not have an ad to show. This is different from the normal container in that the container has a shadow, which would not be shown if `clipsToBounds` was turned on.
  • Loading branch information
larsacus committed Sep 24, 2012
2 parents b90696f + 6389dc0 commit 2eb62fc
Showing 1 changed file with 65 additions and 33 deletions.
98 changes: 65 additions & 33 deletions LARSAdController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ @interface LARSAdController()
@property (nonatomic,
getter = isRegisteredForOrientationChanges) BOOL registeredForOrientationChanges;

/*
Contains the ads so they will clip since the outer container does not clip subviews to retain shadows
*/
@property (strong, nonatomic) UIView *clippingContainer;

- (void)createGoogleAds;

- (void)destroyIAds;
Expand All @@ -42,20 +47,6 @@ - (void)handleOrientationNotification:(NSNotification *)orientationNotification;

@implementation LARSAdController

@synthesize iAdBannerView = _iAdBannerView;
@synthesize googleAdBannerView = _googleAdBannerView;
@synthesize parentView = _parentView;
@synthesize googleAdVisible = _googleAdVisible;
@synthesize iAdVisible = _iAdVisible;
@synthesize parentViewController = _parentViewController;
@synthesize googleAdPublisherId = _googleAdPublisherId;
@synthesize lastOrientationWasPortrait = _lastOrientationWasPortrait;
@synthesize currentOrientation = _currentOrientation;
@synthesize anyAdsVisible = _anyAdsVisible;
@synthesize shouldHandleOrientationChanges = _shouldHandleOrientationChanges;
@synthesize containerView = _containerView;
@synthesize registeredForOrientationChanges = _registeredForOrientationChanges;

CGFloat const kLARSAdContainerHeightPad = 90.0f;
CGFloat const kLARSAdContainerHeightPod = 50.0f;

Expand Down Expand Up @@ -125,7 +116,7 @@ - (void)addAdContainerToView:(UIView *)view withParentViewController:(UIViewCont
self.parentViewController = viewController;
self.parentView = view;

[self.containerView addSubview:self.iAdBannerView];
[self.clippingContainer addSubview:self.iAdBannerView];
[self fixAdContainerFrame];
[view addSubview:self.containerView];
}
Expand Down Expand Up @@ -162,6 +153,14 @@ - (UIView *)containerView{
_containerView.layer.shadowOffset = CGSizeMake(0.f, 0.f);
_containerView.layer.shouldRasterize = YES;
_containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

_clippingContainer = [[UIView alloc] initWithFrame:_containerView.bounds];
self.clippingContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.clippingContainer.backgroundColor = [UIColor clearColor];
self.clippingContainer.clipsToBounds = YES;
self.clippingContainer.userInteractionEnabled = YES;

[_containerView addSubview:self.clippingContainer];
}
return _containerView;
}
Expand Down Expand Up @@ -259,19 +258,31 @@ - (void)bannerViewDidLoadAd:(ADBannerView *)banner{
[self destroyGoogleAdsAnimated:YES];

if (!self.isIAdVisible) {
CGRect newFrame;
newFrame.origin = CGPointMake(CGRectGetMinX(banner.frame), CGRectGetHeight(self.containerView.frame) - CGRectGetHeight(banner.frame));
newFrame.size = banner.frame.size;

[UIView animateWithDuration:0.250
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.0, -banner.frame.size.height)];
banner.frame = newFrame;
}
completion:^(BOOL finished){
self.iAdVisible = YES;
self.anyAdsVisible = (self.isIAdVisible || self.isGoogleAdVisible);
self.containerView.userInteractionEnabled = YES;
}
];

#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"%@: iAd frame after ad load: %@", NSStringFromClass([self class]), NSStringFromCGRect(newFrame));
#endif
}

#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"%@: iAd did load ad", NSStringFromClass([self class]));
#endif
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave{
Expand All @@ -286,18 +297,25 @@ - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *
//if google ad instance is nil
// create new instance of google ad
if (self.isIAdVisible) {
CGRect newFrame;
newFrame.origin = CGPointMake(CGRectGetMinX(banner.frame), CGRectGetHeight(self.containerView.frame));
newFrame.size = banner.frame.size;

[UIView animateWithDuration:0.250
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.0, banner.frame.size.height)];
banner.frame = newFrame;
}
completion:^(BOOL finished){
self.iAdVisible = NO;
self.anyAdsVisible = (self.isIAdVisible || self.isGoogleAdVisible);
self.containerView.userInteractionEnabled = NO;//google ad will re-enable userInteraction when necessary
}
];
#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"%@: iAd frame after ad fail: %@", NSStringFromClass([self class]), NSStringFromCGRect(newFrame));
#endif
}

if (!_googleAdBannerView) {
Expand All @@ -309,8 +327,16 @@ - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *
if(self.containerView.superview != self.parentView){
[self.parentView addSubview:self.containerView];
[self.parentView bringSubviewToFront:self.containerView];
[self.containerView bringSubviewToFront:self.iAdBannerView];
[self.clippingContainer bringSubviewToFront:self.iAdBannerView];
}

#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"%@: iAd frame after ad fail: %@", NSStringFromClass([self class]), NSStringFromCGRect(self.iAdBannerView.frame));
#endif

#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"%@: iAd did fail to receive ad", NSStringFromClass([self class]));
#endif
}

#pragma mark -
Expand Down Expand Up @@ -387,7 +413,7 @@ - (void)createGoogleAds{
self.googleAdBannerView.delegate = self;
[self.googleAdBannerView loadRequest:[GADRequest request]];

[self.containerView insertSubview:self.googleAdBannerView belowSubview:self.iAdBannerView];
[self.clippingContainer insertSubview:self.googleAdBannerView belowSubview:self.iAdBannerView];
}
else if(!_googleAdPublisherId){
NSLog(@"%@ WARNING: Google Ad Publisher ID not set. No ads will be served until you set one using setGoogleAdPublisherId:!", NSStringFromClass(self.class));
Expand All @@ -407,10 +433,12 @@ - (void)destroyGoogleAdsAnimated:(BOOL)animated{
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.googleAdBannerView.frame =
CGRectOffset(self.googleAdBannerView.frame,
0.f,
self.googleAdBannerView.frame.size.height);
CGRect frame;
frame.origin = CGPointMake(self.googleAdBannerView.frame.origin.x,
CGRectGetHeight(self.containerView.frame));
frame.size = self.googleAdBannerView.frame.size;

self.googleAdBannerView.frame = frame;
}
completion:^(BOOL finished){
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -443,14 +471,16 @@ - (void)setGoogleAdPublisherId:(NSString *)publisherId{
#pragma mark -
#pragma mark AdMob/Google Delegate Methods
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView{
CGRect newFrame;
newFrame.origin = CGPointMake(self.iAdBannerView.frame.origin.x,
CGRectGetHeight(self.containerView.frame) - CGRectGetHeight(self.iAdBannerView.frame));
newFrame.size = self.iAdBannerView.frame.size;

[UIView animateWithDuration:0.25f
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.googleAdBannerView.frame =
CGRectOffset(self.googleAdBannerView.frame,
0.f,
-(self.googleAdBannerView.frame.size.height-2.0f));
self.googleAdBannerView.frame = newFrame;
}
completion:^(BOOL finished){
self.googleAdVisible = YES;
Expand All @@ -460,19 +490,21 @@ - (void)adViewDidReceiveAd:(GADBannerView *)bannerView{
}
];
#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"Google ad did receive ad");
NSLog(@"%@: Google ad did receive ad", NSStringFromClass([self class]));
#endif
}
//
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error{
CGRect newFrame;
newFrame.origin = CGPointMake(CGRectGetMinX(self.googleAdBannerView.frame),
CGRectGetHeight(self.containerView.frame));
newFrame.size = self.googleAdBannerView.frame.size;

[UIView animateWithDuration:0.25f
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.googleAdBannerView.frame =
CGRectOffset(self.googleAdBannerView.frame,
0.f,
self.googleAdBannerView.frame.size.height-2.0f);
self.googleAdBannerView.frame = newFrame;
}
completion:^(BOOL finished){
self.googleAdVisible = NO;
Expand All @@ -481,7 +513,7 @@ - (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADReque
}
];
#ifdef LARSADCONTROLLER_DEBUG
NSLog(@"Google ad failed to receive ad");
NSLog(@"%@: Google ad did fail to receive ad", NSStringFromClass([self class]));
#endif
}

Expand Down

0 comments on commit 2eb62fc

Please sign in to comment.