Advertisement
julong

admob

Mar 14th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. 1).h
  2. #import "GADBannerViewDelegate.h"
  3. @class GADBannerView, GADRequest;
  4. <GADBannerViewDelegate>
  5. //admob
  6. @private
  7. GADBannerView *bannerView_;
  8. BOOL isAdPositionAtTop_;
  9. //admob
  10. - (GADRequest *)createRequest;
  11. @property(strong,nonatomic)GADBannerView *bannerView;
  12. 2).m
  13. //admob
  14. #import "GADBannerView.h"
  15. #import "GADRequest.h"
  16. #import "AppID.h"
  17.  
  18. #pragma mark - IntroLayer
  19. #define kHighScoreKey @"com.theme5hq"
  20.  
  21. //admob
  22. typedef enum _bannerType
  23. {
  24. kBanner_Portrait_Top,
  25. kBanner_Portrait_Bottom,
  26. kBanner_Landscape_Top,
  27. kBanner_Landscape_Bottom,
  28. }CocosBannerType;
  29.  
  30. #define BANNER_TYPE kBanner_Landscape_Bottom //change this on need basis
  31.  
  32. //end admob
  33. at interface
  34. CocosBannerType mBannerType;
  35. float on_x, on_y, off_x, off_y;
  36. @synthesize bannerView = bannerView_;
  37. // admob
  38.  
  39. -(void)createAdmobAds
  40. {
  41. mBannerType = BANNER_TYPE;
  42.  
  43. AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
  44. // Create a view of the standard size at the bottom of the screen.
  45. // Available AdSize constants are explained in GADAdSize.h.
  46.  
  47. if(mBannerType <= kBanner_Portrait_Bottom)
  48. bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
  49. else
  50. bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
  51.  
  52. // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
  53. bannerView_.adUnitID = MyUnitId;
  54.  
  55. // Let the runtime know which UIViewController to restore after taking
  56. // the user wherever the ad goes and add it to the view hierarchy.
  57.  
  58. bannerView_.rootViewController = app.navController;
  59. [app.navController.view addSubview:bannerView_];
  60.  
  61. // Initiate a generic request to load it with an ad.
  62. [bannerView_ loadRequest:[GADRequest request]];
  63.  
  64. CGSize s = [[CCDirector sharedDirector] winSize];
  65.  
  66. CGRect frame = bannerView_.frame;
  67.  
  68. off_x = 0.0f;
  69. on_x = 0.0f;
  70.  
  71. switch (mBannerType)
  72. {
  73. case kBanner_Portrait_Top:
  74. {
  75. off_y = -frame.size.height;
  76. on_y = 0.0f;
  77. }
  78. break;
  79. case kBanner_Portrait_Bottom:
  80. {
  81. off_y = s.height;
  82. on_y = s.height-frame.size.height;
  83. }
  84. break;
  85. case kBanner_Landscape_Top:
  86. {
  87. off_y = -frame.size.height;
  88. on_y = 0.0f;
  89. }
  90. break;
  91. case kBanner_Landscape_Bottom:
  92. {
  93. off_y = s.height;
  94. on_y = s.height-frame.size.height;
  95. }
  96. break;
  97.  
  98. default:
  99. break;
  100. }
  101.  
  102. frame.origin.y = off_y;
  103. frame.origin.x = off_x;
  104.  
  105. bannerView_.frame = frame;
  106.  
  107. [UIView beginAnimations:nil context:nil];
  108. [UIView setAnimationDuration:0.2];
  109. [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  110.  
  111. frame = bannerView_.frame;
  112. frame.origin.x = on_x;
  113. frame.origin.y = on_y;
  114.  
  115.  
  116. bannerView_.frame = frame;
  117. [UIView commitAnimations];
  118. }
  119.  
  120.  
  121. -(void)showBannerView
  122. {
  123. if (bannerView_)
  124. {
  125. [UIView animateWithDuration:0.2
  126. delay:0.1
  127. options: UIViewAnimationCurveEaseOut
  128. animations:^
  129. {
  130. CGRect frame = bannerView_.frame;
  131. frame.origin.y = on_y;
  132. frame.origin.x = on_x;
  133.  
  134. bannerView_.frame = frame;
  135. }
  136. completion:^(BOOL finished)
  137. {
  138. }];
  139. }
  140.  
  141. }
  142.  
  143.  
  144. -(void)hideBannerView
  145. {
  146. if (bannerView_)
  147. {
  148. [UIView animateWithDuration:0.2
  149. delay:0.1
  150. options: UIViewAnimationCurveEaseOut
  151. animations:^
  152. {
  153. CGRect frame = bannerView_.frame;
  154. frame.origin.y = off_y;
  155. frame.origin.x = off_x;
  156. }
  157. completion:^(BOOL finished)
  158. {
  159. }];
  160. }
  161.  
  162. }
  163.  
  164. -(void)dismissAdView
  165. {
  166. if (bannerView_)
  167. {
  168. [UIView animateWithDuration:0.2
  169. delay:0.1
  170. options: UIViewAnimationCurveEaseOut
  171. animations:^
  172. {
  173. CGRect frame = bannerView_.frame;
  174. frame.origin.y = off_y;
  175. frame.origin.x = off_x;
  176. bannerView_.frame = frame;
  177. }
  178. completion:^(BOOL finished)
  179. {
  180. [bannerView_ setDelegate:nil];
  181. [bannerView_ removeFromSuperview];
  182. bannerView_ = nil;
  183.  
  184. }];
  185. }
  186. }
  187. //end admob
  188. - (void) dealloc
  189. {
  190. //[viewController release];
  191. bannerView_.delegate = nil;
  192. [bannerView_ release];
  193. [super dealloc];
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement