Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
BasicBanner/BasicBanner/TextViewController.m
/* |
Copyright (C) 2013-2015 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
A simple view controller that manages a content view and an ADBannerView |
*/ |
#import "TextViewController.h" |
@interface TextViewController () |
@property (nonatomic, strong) IBOutlet UIView *contentView; |
@property (nonatomic, strong) IBOutlet UITextView *textView; |
@property (nonatomic, strong) IBOutlet UILabel *timerLabel; |
@end |
@implementation TextViewController { |
ADBannerView *_bannerView; |
NSTimer *_timer; |
CFTimeInterval _ticks; |
} |
- (instancetype)init |
{ |
self = [super initWithNibName:@"TextViewController" bundle:nil]; |
if (self) { |
// On iOS 6 ADBannerView introduces a new initializer, use it when available. |
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) { |
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner]; |
} else { |
_bannerView = [[ADBannerView alloc] init]; |
} |
_bannerView.delegate = self; |
} |
return self; |
} |
- (void)layoutAnimated:(BOOL)animated |
{ |
// As of iOS 6.0, the banner will automatically resize itself based on its width. |
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately. |
CGRect contentFrame = self.view.bounds; |
if (contentFrame.size.width < contentFrame.size.height) { |
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; |
} else { |
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; |
} |
CGRect bannerFrame = _bannerView.frame; |
if (_bannerView.bannerLoaded) { |
contentFrame.size.height -= _bannerView.frame.size.height; |
bannerFrame.origin.y = contentFrame.size.height; |
} else { |
bannerFrame.origin.y = contentFrame.size.height; |
} |
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ |
_contentView.frame = contentFrame; |
[_contentView layoutIfNeeded]; |
_bannerView.frame = bannerFrame; |
}]; |
} |
- (void)setText:(NSString *)text |
{ |
_text = [text copy]; |
self.textView.text = text; |
} |
- (void)startTimer |
{ |
if (_timer == nil) { |
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; |
} |
} |
- (void)stopTimer |
{ |
[_timer invalidate]; |
_timer = nil; |
} |
- (void)timerTick:(NSTimer *)timer |
{ |
// Timers are not guaranteed to tick at the nominal rate specified, so this isn't technically accurate. |
// However, this is just an example to demonstrate how to stop some ongoing activity, so we can live with that inaccuracy. |
_ticks += 0.1; |
double seconds = fmod(_ticks, 60.0); |
double minutes = fmod(trunc(_ticks / 60.0), 60.0); |
double hours = trunc(_ticks / 3600.0); |
self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds]; |
} |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
[self.view addSubview:_bannerView]; |
self.textView.text = self.text; |
} |
- (void)viewDidAppear:(BOOL)animated |
{ |
[super viewDidAppear:animated]; |
[self layoutAnimated:NO]; |
[self startTimer]; |
} |
- (void)viewDidDisappear:(BOOL)animated |
{ |
[super viewDidDisappear:animated]; |
[self stopTimer]; |
} |
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
{ |
return YES; |
} |
#endif |
- (NSUInteger)supportedInterfaceOrientations |
{ |
return UIInterfaceOrientationMaskAll; |
} |
- (void)viewDidLayoutSubviews |
{ |
[self layoutAnimated:[UIView areAnimationsEnabled]]; |
} |
- (void)bannerViewDidLoadAd:(ADBannerView *)banner |
{ |
[self layoutAnimated:YES]; |
} |
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error |
{ |
[self layoutAnimated:YES]; |
} |
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave |
{ |
[self stopTimer]; |
return YES; |
} |
- (void)bannerViewActionDidFinish:(ADBannerView *)banner |
{ |
[self startTimer]; |
} |
@end |
Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-05-28