Saturday, June 26, 2010

Ad Viewer: Supporting rotation, adding clear button

The next step of my adviser program is to support rotation of the view, and move stuff back into place.  I ran into a problem briefly where I overrode the loadVIew method, that caused NOTHING to work.

I implemented the following methods to enable more than one ad to appear on the page (I have no idea if apple will allow this), and to support rotation.  Finally I added a clear button, so that more ads can be requested.

Below are screen shots in both portrait and landscape mode.







The following is the code.

When I first wrote this code I was worried about the resolution difference between the new IPhone 4 and older iphones, so I made sure to use frames as generated by the iPhone os.  I later read in the documentation that the iPhone OS is now using points instead of pixels.  This will automatically scale for the hardware factor.

I'm leaving the code as is, so that if Apple creates additional ad sizes, the code will work fine.  Right now with the screen sizes, 7 ads can fit in both of my dialogs.

The Header


My header defines a screen area for ads, an action for clearing ads.

IBOutlet UIView* bannerViews;
NSMutableArray *currentAds;
}
@property (nonatomic,retain) UIView *bannerViews;
@property (nonatomic,retain) NSMutableArray *currentAds;
// Clear all ads, and request new ones.
- (IBAction) clearAds:(id)sender;
- (void) moveAds;

Setting up the views


I created a routine to make the banner views, it initializes it for both portrait and landscape, as well as choosing the proper orientation for the current view.:
- (ADBannerView *) createBannerView
{
ADBannerView *view=[[ADBannerView alloc]init];
view.requiredContentSizeIdentifiers=[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32,nil];
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
view.currentContentSizeIdentifier=ADBannerContentSizeIdentifier480x32;
else {
view.currentContentSizeIdentifier=ADBannerContentSizeIdentifier320x50;
}
return view;
}
The viewDidLoad calls clear ads to set up the initial set of banners.  DO NOT USE loadView.  Then your nib is not loaded.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 [self clearAds:self];
}


The clear ads callback erases all current ads and then creates new ads to fit the bannerViews UIView.  

- (IBAction) clearAds:(id)sender
{
if (currentAds!=nil)
{
for (ADBannerView*view in currentAds)
{
[view removeFromSuperview];
}
}
currentAds=nil;

currentAds=[[NSMutableArray alloc] init];
ADBannerView *currentView= [self createBannerView];
int y=0;
while (y <bannerViews.bounds.size.height)
{
[bannerViews addSubview:currentView];

NSLog(@"using y %d",y);
currentView.frame =CGRectMake(currentView.frame.origin.x, y+bannerViews.bounds.origin.y
  currentView.frame.size.width
  currentView.frame.size.height);
[currentAds addObject:currentView];
y+= currentView.bounds.size.height;
if (y!=0)
{
currentView=[self createBannerView];
}
}
}

Handling rotation


The rotation is fairly simple under willRotate I set all the banner bars to the proper orientation.  Then in didRotate, I animate a change to the new spots.

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSString *newOrientation;
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
newOrientation=ADBannerContentSizeIdentifier480x32;
else {
newOrientation=ADBannerContentSizeIdentifier320x50;
}
if (currentAds==nil)
return;
[UIView beginAnimations:@"rotateView" context:NULL];
for (ADBannerView *view in currentAds)
{
view.currentContentSizeIdentifier=newOrientation;
}
[UIView commitAnimations];

}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[UIView beginAnimations:@"rotateView" context:NULL];
[self moveAds];
[UIView commitAnimations];
}

Moving views to their new spots

- (void) moveAds
{
int y=0;
for (ADBannerView * currentView in currentAds)
{
currentView.frame =CGRectMake(currentView.frame.origin.x, y+bannerViews.bounds.origin.y
  currentView.frame.size.width
  currentView.frame.size.height);
y+= currentView.bounds.size.height;
}
}

No comments:

Post a Comment