/////in .h file
///framework /library "iAd" add in app
#import "iAd/ADBannerView.h"
//// JP -19-1-2012
....UIViewController<ADBannerViewDelegate>{
UIView *_contentView;
id _adBannerView;
BOOL _adBannerViewIsVisible;
NSString *frameflag;
}
@property (nonatomic , retain) UIView *contentView;
@property (nonatomic, retain) id adBannerView;
@property (nonatomic) BOOL adBannerViewIsVisible;
@end
//in .m file
@synthesize adBannerView = _adBannerView;
@synthesize adBannerViewIsVisible= _adBannerViewIsVisible;
@synthesize contentView = _contentView;
- (void)viewDidLoad {
isDapAFriend=FALSE;
[self createAdBannerView];
}
- (void)viewWillAppear:(BOOL)animated
{
[self fixupAdView:[UIDevice currentDevice].orientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self fixupAdView:toInterfaceOrientation];
}
/////////////////////////// JP -19-1-2012 Start
#pragma iAd Delegate Methods
- (int)getBannerHeight:(UIDeviceOrientation)orientation {
if (UIInterfaceOrientationIsLandscape(orientation)) {
return 32;
} else {
return 50;
}
}
- (int)getBannerHeight {
return [self getBannerHeight:[UIDevice currentDevice].orientation];
}
- (void)createAdBannerView {
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil) {
// self.adBannerView = [[[classAdBannerView alloc]
// initWithFrame:CGRectMake(0, self.view.frame.size.height - [self getBannerHeight], 320, [self getBannerHeight])] autorelease];
self.adBannerView = [[[classAdBannerView alloc]
initWithFrame:CGRectMake(0, 480, 320, 50)] autorelease];
[_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil]];
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier480x32];
} else {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier320x50];
}
[self.adBannerView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin];
// [_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
// -[self getBannerHeight])];
[_adBannerView setDelegate:self];
[self.view addSubview:_adBannerView];
}
}
- (void)fixupAdView:(UIInterfaceOrientation)toInterfaceOrientation {
if (_adBannerView != nil) {
// UIView *myView = self.view;
//
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.3];
// CGRect newFrame = myView.frame;
// newFrame.size.height = newFrame.size.height -[self getBannerHeight];
// myView.frame = newFrame;
// [UIView commitAnimations];
if ([frameflag isEqualToString:@"False"]) {
frameflag = @"True";
for (UIView *view in [self.view subviews]) {
if ([view class] == [UIButton class]|| [view class] == [UIImageView class]){
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - 15, view.frame.size.width, view.frame.size.height);
}
}
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier480x32];
} else {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier320x50];
}
[UIView beginAnimations:@"fixupViews" context:nil];
if (_adBannerViewIsVisible) {
CGRect adBannerViewFrame = [_adBannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = self.view.frame.size.height - [self getBannerHeight:toInterfaceOrientation];
[_adBannerView setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = [self getBannerHeight:toInterfaceOrientation];
contentViewFrame.size.height = self.view.frame.size.height - [self getBannerHeight:toInterfaceOrientation];
_contentView.frame = contentViewFrame;
} else {
CGRect adBannerViewFrame = [_adBannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = self.view.frame.size.height + [self getBannerHeight:toInterfaceOrientation];
[_adBannerView setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = 480;
contentViewFrame.size.height = self.view.frame.size.height;
_contentView.frame = contentViewFrame;
}
[UIView commitAnimations];
}
}
#pragma mark ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!_adBannerViewIsVisible) {
_adBannerViewIsVisible = YES;
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
[self fixupAdView:[UIDevice currentDevice].orientation];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (_adBannerViewIsVisible)
{
_adBannerViewIsVisible = NO;
[self fixupAdView:[UIDevice currentDevice].orientation];
}
}
/////////////////////////// JP -19-1-2012 End
- (void)dealloc {
self.contentView = nil;
self.adBannerView = nil;
[super dealloc];
}
@end
No comments:
Post a Comment