我有一個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
已下載示例,它顯示橫幅在底部,而不是在中間。 – Guru
這就是奇怪的測試它在所有ipad和它總是在屏幕中間 – claustro