admob位于屏幕中间?

时间:2014-06-05 13:47:36

标签: ipad cocos2d-iphone admob

我有一个针对ipad的admob定位问题。在ipad上运行时,横幅的位置位于屏幕中间。 Android和iphone底部显示横幅。在调试后CGSize s = [[CCDirector sharedDirector] viewSize];似乎认为它是一部iphone。知道为什么吗?I am using this sample

它是一个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

2 个答案:

答案 0 :(得分:1)

在我找到这个之前我的情况一样。 @macgeo你的解决方案确实有效,我不确定为什么,但它确实将大小恢复为适用于ipad设备的值

附加信息:使用SpriteBuilder在纵向布局上生成内容大小为(384,512),内容比例因子为4.内容比例因子对视图定位的结果没有影响,如将大小更改为

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

生成大小为(768,1024),如果它使用的内容比例因子为2,但仍然是4。

答案 1 :(得分:0)

可以通过将viewSize更改为view.bounds

来解决思考问题
//CGSize s = [[CCDirector sharedDirector] viewSize];
CGRect contentFrame = [CCDirector sharedDirector].view.bounds;

然后使用contentFrame.size.height进行y定位。在使用iAd时遇到了麻烦,这似乎解决了这个问题。