2014-06-05 49 views
0

我有一個iPad的定位問題admob。在iPad上運行時,橫幅的位置位於屏幕中間。 Android和iPhone在底部顯示橫幅。調試後CGSize s = [[CCDirector sharedDirector] viewSize];似乎認爲它是一個iphone。任何想法爲什麼? I am using this sampleadmob定位在屏幕中間?

它是一個cocos2d項目。

AppDelegate.h 
#define ADMOB_BANNER_UNIT_ID ((IS_IPAD) ? @"a1526954f69b314" : @"a1526955dc20272"); 

#import "GADBannerView.h" 
typedef enum _bannerType 
{ 
    kBanner_Portrait_Top, 
    kBanner_Portrait_Bottom, 
    kBanner_Landscape_Top, 
    kBanner_Landscape_Bottom, 
}CocosBannerType; 
#define BANNER_TYPE kBanner_Portrait_Bottom 


@interface AppController : CCAppDelegate<GADBannerViewDelegate> { 

    CocosBannerType mBannerType; 
    GADBannerView *mBannerView; 
    float on_x, on_y, off_x, off_y;} 
-(void)hideBannerView; 
-(void)showBannerView; 

@end 
AppDelegate.m 
@implementation AppController 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Configure Cocos2d with the options set in SpriteBuilder 
    NSString* configPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Published-iOS"]; // TODO: add support for Published-Android support 
    configPath = [configPath stringByAppendingPathComponent:@"configCocos2d.plist"]; 

    NSMutableDictionary* cocos2dSetup = [NSMutableDictionary dictionaryWithContentsOfFile:configPath]; 

    // Note: this needs to happen before configureCCFileUtils is called, because we need apportable to correctly setup the screen scale factor. 
#ifdef APPORTABLE 
    if([cocos2dSetup[CCSetupScreenMode] isEqual:CCScreenModeFixed]) 
     [UIScreen mainScreen].currentMode = [UIScreenMode emulatedMode:UIScreenAspectFitEmulationMode]; 
    else 
     [UIScreen mainScreen].currentMode = [UIScreenMode emulatedMode:UIScreenScaledAspectFitEmulationMode]; 
#endif 

    // Configure CCFileUtils to work with SpriteBuilder 
    [CCBReader configureCCFileUtils]; 

    // Do any extra configuration of Cocos2d here (the example line changes the pixel format for faster rendering, but with less colors) 
    //[cocos2dSetup setObject:kEAGLColorFormatRGB565 forKey:CCConfigPixelFormat]; 

    [self setupCocos2dWithOptions:cocos2dSetup]; 

#ifndef APPORTABLE 

    [[AAGameCenter sharedGameCenter] authenticateLocalUser]; 
#endif 
    [self createAdmobAds]; 


    return YES; 


} 


- (CCScene*) startScene 
{ 
    return [CCBReader loadAsScene:@"MainMenuScene"]; 


} 
+ (void)initialize 
{ 

#ifndef APPORTABLE 
    //configure iRate 
    [iRate sharedInstance].appStoreID = 87522385224; // Replace this 
#endif 
} 
-(void)createAdmobAds 
{ 
    mBannerType = BANNER_TYPE; 

    if(mBannerType <= kBanner_Portrait_Bottom) 
     mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait]; 
    else 
     mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape]; 

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID. 

    mBannerView.adUnitID = @"ca-app-pub-885995432323732999994/1885724261"; 


    // Let the runtime know which UIViewController to restore after taking 
    // the user wherever the ad goes and add it to the view hierarchy. 

    mBannerView.rootViewController = self.navController; 
    [self.navController.view addSubview:mBannerView]; 

    //#ifdef DEBUG 
    // GADRequest *request = [GADRequest request]; 
    // request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil]; 
    //#endif 


    // Initiate a generic request to load it with an ad. 
    [mBannerView loadRequest:[GADRequest request]]; 

    CGSize s = [[CCDirector sharedDirector] viewSize]; 

    CGRect frame = mBannerView.frame; 

    off_x = 0.0f; 
    on_x = 0.0f; 

    switch (mBannerType) 
    { 
     case kBanner_Portrait_Top: 
     { 
      off_y = -frame.size.height; 
      on_y = 0.0f; 
     } 
      break; 
     case kBanner_Portrait_Bottom: 
     { 
      off_y = s.height; 
      on_y = s.height-frame.size.height; 
     } 
      break; 
     case kBanner_Landscape_Top: 
     { 
      off_y = -frame.size.height; 
      on_y = 0.0f; 
     } 
      break; 
     case kBanner_Landscape_Bottom: 
     { 
      off_y = s.height; 
      on_y = s.height-frame.size.height; 
     } 
      break; 

     default: 
      break; 
    } 

    frame.origin.y = off_y; 
    frame.origin.x = off_x; 

    mBannerView.frame = frame; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseOut]; 

    frame = mBannerView.frame; 
    frame.origin.x = on_x; 
    frame.origin.y = on_y; 


    mBannerView.frame = frame; 
    [UIView commitAnimations]; 
} 


-(void)showBannerView 
{ 
    if (mBannerView) 
    { 
     //banner on bottom 
     { 
      CGRect frame = mBannerView.frame; 
      frame.origin.y = off_y; 
      frame.origin.x = on_x; 
      mBannerView.frame = frame; 


      [UIView animateWithDuration:0.5 
            delay:0.1 
           options: UIViewAnimationOptionCurveEaseOut 
          animations:^ 
      { 
       CGRect frame = mBannerView.frame; 
       frame.origin.y = on_y; 
       frame.origin.x = on_x; 

       mBannerView.frame = frame; 
      } 
          completion:^(BOOL finished) 
      { 
      }]; 
     } 
     //Banner on top 
     //  { 
     //   CGRect frame = mBannerView.frame; 
     //   frame.origin.y = -frame.size.height; 
     //   frame.origin.x = off_x; 
     //   mBannerView.frame = frame; 
     // 
     //   [UIView animateWithDuration:0.5 
     //         delay:0.1 
     //        options: UIViewAnimationCurveEaseOut 
     //        animations:^ 
     //    { 
     //     CGRect frame = mBannerView.frame; 
     //     frame.origin.y = 0.0f; 
     //     frame.origin.x = off_x; 
     //     mBannerView.frame = frame; 
     //    } 
     //        completion:^(BOOL finished) 
     //    { 
     // 
     // 
     //    }]; 
     //  } 

    } 

} 


-(void)hideBannerView 
{ 
    if (mBannerView) 
    { 
     [UIView animateWithDuration:0.5 
           delay:0.1 
          options: UIViewAnimationCurveEaseOut 
         animations:^ 
     { 
      CGRect frame = mBannerView.frame; 
      frame.origin.y = off_y; 
      frame.origin.x = off_x; 
      mBannerView.frame = frame; 
     } 
         completion:^(BOOL finished) 
     { 


     }]; 
    } 
} 


-(void)dismissAdView 
{ 
    if (mBannerView) 
    { 
     [UIView animateWithDuration:0.5 
           delay:0.1 
          options: UIViewAnimationCurveEaseOut 
         animations:^ 
     { 
      CGSize s = [[CCDirector sharedDirector] viewSize]; 

      CGRect frame = mBannerView.frame; 
      frame.origin.y = frame.origin.y + frame.size.height ; 
      frame.origin.x = (s.width/2.0f - frame.size.width/2.0f); 
      mBannerView.frame = frame; 
     } 
         completion:^(BOOL finished) 
     { 
      [mBannerView setDelegate:nil]; 
      [mBannerView removeFromSuperview]; 
      mBannerView = nil; 

     }]; 
    } 

} 


@end 
+0

已下載示例,它顯示橫幅在底部,而不是在中間。 – Guru

+0

這就是奇怪的測試它在所有ipad和它總是在屏幕中間 – claustro

回答

0

思考問題可能通過改變viewSize被固定到view.bounds

//CGSize s = [[CCDirector sharedDirector] viewSize]; 
CGRect contentFrame = [CCDirector sharedDirector].view.bounds; 

,然後使用contentFrame.size.height你Y定位。在使用iAd的東西時遇到了問題,而且似乎解決了這個問題。

1

我有同樣的情況,直到我找到這個。 @macgeo您的解決方案確實有效,但我不確定爲什麼,但它確實將大小恢復爲適用於ipad設備的值。

其他信息:使用SpriteBuilder在縱向佈局上生成內容大小(384,512)與爲4的含量比例因子的含量比例因子具有在視圖定位的結果來改變大小時,

CGRect contentFrame = [CCDirector sharedDirector].view.bounds; 

產生的尺寸(768,1024)一樣,如果它被利用,因爲沒有影響內容比例因子爲2,但仍然爲4.