- //
- // BannerViewController.h
- //
- #import <UIKit/UIKit.h>
- #import <iAd/iAd.h>
- #import <iAd/ADBannerView.h>
- #define kUp 1
- #define kDown (-1)
- #define bannerView_h 50
- #define kBannerFrameUnderNav CGRectMake(0, -50, 320, 50)
- #define kBannerFrameAboveTab CGRectMake(0, 367, 320, 50)
- @protocol ADDelegate
- @optional
- //在此方法中调整界面,当b为YES时,表示广告加载成功,调整界面,为NO时失败,界面调回原样
- - (void)ADIsComing:(BOOL)b;
- @end
- @interface BannerViewController : NSObject <ADBannerViewDelegate> {
- id <ADDelegate> delegate;
- NSInteger upOrDown; //当广告出现时view向上还是向下滑动
- ADBannerView *view;
- }
- @property (nonatomic, assign) id<ADDelegate> delegate;
- @property (nonatomic, assign) NSInteger upOrDown;
- @property (nonatomic, assign) CGRect frame;
- @property (nonatomic, retain) ADBannerView *view;
- + (id) sharedBanner;
- + (void) releaseSharedBanner;
- - (id)initWithFrame:(CGRect)rect;
- - (BOOL) isBannerLoaded;
- @end
- //
- // BannerViewController.m
- //
- //
- // Created by on 9/30/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "BannerViewController.h"
- BOOL receiveAd = NO;
- BannerViewController *bannerViewController;
- @implementation BannerViewController
- @synthesize delegate;
- @synthesize upOrDown;
- @synthesize view;
- + (id) sharedBanner {
- if (bannerViewController == nil) {
- bannerViewController = [[[self class] alloc] initWithFrame:kBannerFrameUnderNav];
- bannerViewController.upOrDown = kUp;
- }
- return bannerViewController;
- }
- + (void) releaseSharedBanner {
- [bannerViewController release];
- }
- - (id)initWithFrame:(CGRect)rect {
- self = [super init];
- if (self != nil) {
- self.view = [[[ADBannerView alloc] initWithFrame:rect] autorelease];
- view.delegate = self;
- }
- return self;
- }
- - (void)setDelegate:(id <ADDelegate>)d {
- if (delegate == d) return;
- if (view.isBannerLoaded) {
- if (delegate != nil) {
- [delegate ADIsComing:NO];
- }
- [d ADIsComing:YES];
- }
- delegate = d;
- }
- - (CGRect)frame {
- return view.frame;
- }
- - (void)setFrame:(CGRect)r {
- view.frame = r;
- }
- - (BOOL) isBannerLoaded {
- return [(ADBannerView *)self.view isBannerLoaded];
- }
- #pragma mark - adbanner view delegage
- - (void)bannerViewDidLoadAd:(ADBannerView *)banner {
- if (receiveAd == NO) {
- receiveAd = YES;
- [delegate ADIsComing:YES];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.3];
- banner.frame = CGRectOffset(banner.frame, 0, upOrDown * (banner.frame.size.height + 12));
- [UIView commitAnimations];
- }
- }
- - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
- if (receiveAd == YES) {
- receiveAd = NO;
- [delegate ADIsComing:NO];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.3];
- banner.frame = CGRectOffset(banner.frame, 0, -(upOrDown * (banner.frame.size.height + 12)));
- [UIView commitAnimations];
- }
- }
- - (void)bannerViewActionDidFinish:(ADBannerView *)banner {
- }
- - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
- return YES;
- }
- - (void)dealloc {
- self.view = nil;
- [super dealloc];
- }
- @end
使用方法:
1:在viewDidAppear:方法中添加以下代码,共享广告,设置委托为self
- - (void)viewDidAppear:(BOOL)animated
- {
- #ifdef CONTRACTIONM_FREE_2_0_
- BannerViewController *banner = [BannerViewController sharedBanner];
- self.bannerViewController = banner;
- banner.delegate = self;
- banner.upOrDown = kUp;
- [self.view addSubview:self.bannerViewController.view];
- #endif
- [super viewDidAppear:animated];
- }
2:在委托方法中根据b的值 ,调整界面
- #ifdef CONTRACTIONM_FREE_2_0_
- - (void)ADIsComing:(BOOL)b
- {
- if (b == YES) {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
- self.settingsScrollView.frame = CGRectMake(0, 60, 320, 356);
- self.settingsScrollView.contentOffset = CGPointMake(0, 50);
- [self.settingsScrollView flashScrollIndicators];
- [self.settingsScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
- }
- }
- else {
- self.settingsScrollView.frame = CGRectMake(0, 0, 320, 416);
- }
- }
- #endif
3:少了一步最最最重要的,我一直浪费了一天在这个上面,气死我啦,在页面跳转的时候把委托置为nil
- -(void)viewWillDisappear:(BOOL)animated
- {
- self.bannerViewController.delegate = nil;
- [super viewWillDisappear:animated];
- }