Why does iAd banner interferes with my UIView animations - ios

I have an iAd that I created following this tutorial and for some reason when the iAd shows-up any animation going on at that time stops. In other words the animations work fine until the iAd apears and as soon as the iAd apears any animation going at that moment stops or it goes back to its original position without finishing the animation.
This is the code I have for the iAd
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
And this is one of the animations I have
- (IBAction)showHideBlackboard:(id)sender
{
[UIView animateWithDuration:0.6 delay: 0.0 options: UIViewAnimationOptionCurveEaseIn animations:^
{
self.controlsView.frame= CGRectMake(10, 10, 250, 250);
}completion:^(BOOL finished)
{
[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
self.drawingView.alpha = 1.0;
self.drawingView.frame= CGRectMake(10, 10, 350, 350);
}completion:nil ];
}];
}
Any idea why does the iAd interferes with UIView animations?

I recently had a similar problem. It turned out that avoiding the option
self.translatesAutoresizingMaskIntoConstraints = NO;
in my UIView subclass solved the problem.

Related

Trouble executing all animation's using animateWithDuration

I'm having some issues with my animations. The intended result is that the UIButton's should both move to their positions, and once they reach their positions they must oscillate upwards and downwards, subtly, to simulate reaching the end of their bungee chord, if you will. The result I'm achieving is that every animation apart from the oscillation is executing perfectly. Why is this? (The oscillation animation I refer to is, of course, the spring animateWithDuration method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_orText.alpha = 0;
//Email login button properties
_emailLoginForwardButton.layer.cornerRadius = _emailLoginForwardButton.frame.size.width / 2;
self.emailLoginForwardButton.layer.borderWidth = 1.5f;
self.emailLoginForwardButton.layer.borderColor = [UIColor whiteColor].CGColor;
[self.emailLoginForwardButton setImageEdgeInsets:UIEdgeInsetsMake(7, 7, 7, 7)];
_emailLoginForwardButton.frame = CGRectMake(77, 700, 40, 40);
[UIView animateWithDuration:0.5
animations:^
{
_emailLoginForwardButton.frame = CGRectMake(77, 470, 40, 40);
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:0.3
delay:0.6
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^(void){}
completion:^(BOOL finished)
{
_orText.text = #"or";
[UIView animateWithDuration:0.4
animations:^
{
[_orText setAlpha:1];
}
];
}
];
}
];
//Google login button properties
_googleLoginForwardButton.layer.cornerRadius =_googleLoginForwardButton.frame.size.width / 2;
self.googleLoginForwardButton.layer.borderWidth = 1.5f;
self.googleLoginForwardButton.layer.borderColor = [UIColor grayColor].CGColor;
_googleLoginForwardButton.frame = CGRectMake(257, 700, 40, 40);
[UIView animateWithDuration:0.5
delay:0.4
options:0
animations:^
{
_googleLoginForwardButton.frame = CGRectMake(257, 470, 40, 40);
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:0.3
delay:0.3
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^
{
}
completion:^(BOOL finished)
{
}
];
}
];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.googleLoginForwardButtonBottomConstraints.constant = -100.0;//e.g
self.emailLoginForwardButtonBottomConstraints.constant = -100.0;//e.g
[UIView animateWithDuration:0.6
delay:0.6
usingSpringWithDamping:0.2
initialSpringVelocity:0.4
options:0
animations:^(void){
self.googleLoginForwardButtonBottomConstraints.constant = 40.0;//e.g
self.emailLoginForwardButtonBottomConstraints.constant = 40.0;//e.g
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
}
I have run this code and its working. Hope it will help you.
[UIView animateWithDuration:1
delay:0.6
usingSpringWithDamping:0.42
initialSpringVelocity:0.1
options:0
animations:^(void)
{
_orText.text = #"or";
[UIView animateWithDuration:0.7
animations:^
{
[_orText setAlpha:1];
}];
[UIView animateWithDuration:0.6
animations:^
{
_emailLoginForwardButton.frame = CGRectMake(77, 470, 40, 40);
}];
[UIView animateWithDuration:0.6
animations:^
{
_googleLoginForwardButton.frame = CGRectMake(257, 470, 40, 40);
}
];
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
This was my solution, however there was more I wanted to do with this code. I wanted to make both buttons move at different intervals, and an if statement won't work here, for some reason. Also, I only wanted this to initialise when another action has taken place, which is still possible to do, but I'd prefer to be able to make them move at different times.

Hide iAd view on pressing a button

I can't find the right solution to hide my iAd view when I press a button.
I am loding iAd view like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
It works perfect. But how would i hide my iAd view if I don't want to show one?
I was searching for answer here, but there was no direct answer.
If the user doesn't have internet connection the banner will be blank. It is required by apple for the banner to be hidden when internet connection is not availble.
Consider you are creating this way,
- (void)createAdBannerView {
Class classAdBannerView = NSClassFromString(#"ADBannerView");
if (classAdBannerView != nil) {
self.adBannerView = [[[classAdBannerView alloc]
initWithFrame:CGRectZero] autorelease];
[_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil]];
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier480x32];
} else {
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier320x50];
}
[_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
-[self getBannerHeight])];
[_adBannerView setDelegate:self];
[self.view addSubview:_adBannerView];
}
}
call [self.adBannerView removeFromSuperview]; when and were you need to hide.
#Matiass21, write the below code here _bannerIsVisible is bool value and _adBanner is ADBannerView object.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"Failed to retrieve ad");
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}

2 iAds on simulator

I am trying to create 2 iAds but I have only one on simulator. I can not now check it with device. Why I have only one? I have only second iAd
My code is:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView1.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
adView2 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView2.frame = CGRectOffset(adView2.frame, 0, 200);
adView2.delegate = self;
[self.backgroundView addSubview:adView2];
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
if (banner == adView1)
{
banner.frame = CGRectOffset(banner.frame, 0, 50);
}
if (banner == adView2)
{
banner.frame = CGRectOffset(banner.frame, 0, 200);
}
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction)
{
[audio pause];
}
return shouldExecuteAction;
}
(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[audio resume];
}
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
It must be CGRectOffset(adView1.frame, 0, 50); since adView2 is allocation in the next line only???
When you are creating first view, you are using second view's frame:
adView1 = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
adView1.frame = CGRectOffset(adView2.frame, 0, 50);
adView1.delegate = self;
[self.backgroundView addSubview:adView1];
As adView2 is not created yet - it is nil and frame is (0,0,0,0)
You have to use adView1 frame for offset as you do for second view.
Also, keep in mind that displaying 2 banners is going against Apple guideline and not recommend. Take a look here (Best practices section)
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html

Animations not holding after iAd is pressed

So, I am trying to implement iAds into my application, but I cannot get it to work properly.
I am using these two functions to show and hide the banner and it works great:
- (void)showAdBanner:(ADBannerView *)banner {
if (bannerVisible) return;
NSLog(#"showing ad");
[UIView beginAnimations:#"animateBannerIn" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[banner setFrame:CGRectOffset([banner frame], 0, -bannerHeight)];
[toolbarView setFrame:CGRectOffset([toolbarView frame], 0, -bannerHeight)];
[UIView commitAnimations];
bannerVisible = YES;
}
- (void)hideAdBanner:(ADBannerView *)banner {
if (!bannerVisible) return;
NSLog(#"hiding ad");
[UIView beginAnimations:#"animateBannerOut" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[banner setFrame:CGRectOffset([banner frame], 0, bannerHeight)];
[toolbarView setFrame:CGRectOffset([toolbarView frame], 0, bannerHeight)];
[UIView commitAnimations];
bannerVisible = NO;
}
The only issue is that after one presses on the iAd banner, the toolbar view returns to its original position sans-iAd.
I have removed all layout constraints and it still is occurring. I can attach a video after if it clarifies.
Thank you
Video: https://www.dropbox.com/s/64mbiowk94sl6rq/iAdIssue.mov

Hide both Navigation Controller and Tab Bar Controller with animation

I'm trying to hide a UITabBarController and UINavigationController simultaneously when a button is touch. I found a very nice code snippet here How to hide uitabbarcontroller but I have problem when trying to hide and animate both UINavigationController and the tabbarcontroller. I also found a lot of examples on the internet when they hide the tabbar using self.tabBarController.tabBar.hidden = YES but that only hides the button items not the black bar at the bottom.
After playing a lot around I can get to make both animate correctly because I think that it's related to the hiding of the Navigation Controller which makes the size of the whole window to change on the fly.
-(IBAction)touchImage:(id)sender {
if (isImageFullScreen) {
isImageFullScreen = NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,20,320,92);
[self showTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
} else {
isImageFullScreen = YES;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,0,320,480);
[self hideTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
}
}
The hideTabBar and showTabBar methods are the ones from the other post I linked above.
I also tried some other combinations but I can't make it look good. Any ideas?
Thanks in advance.
I tried that code now and I see that the UITabBar show animation doesn't take place smoothly.
I managed to make it smoother by adjusting the duration period for the tabbar showing animation to be lower.
[UIView setAnimationDuration:0.2];
Hopefully that works.
EDIT:
Please try this code, it resizes the parent view to be bigger in 1 animation transaction in such a way that only the bars are hidden and the content is shown.
- (IBAction)TestButton1:(UIButton *)sender {
if(!isAnimating){
if(isTabBarAndNavBarHidden){
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=NO;
isAnimating=NO;
}];
}else{
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=YES;
isAnimating=NO;
}];
}
}
}
This code is for iPhone 4/4S.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset > scrollView.contentOffset.y)
{
NSLog(#"Scrolling up");
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
//
}];
}];
}
else if (self.lastContentOffset < scrollView.contentOffset.y)
{
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
}];
}];
NSLog(#"Scrolling Down");
}
self.lastContentOffset = scrollView.contentOffset.y;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
// Do any additional setup after loading the view.
}

Resources