Frage

I have a UIViewController class called "ViewController" and it accesses several SKScenes.

I have these two functions to present and hide ads:

-(void)showBannerView{
    if (_adBanner && !_adBannerViewIsVisible){
        _adBannerViewIsVisible = true;

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        CGRect frame = _adBanner.frame;
        frame.origin.x = 0.0f;
        frame.origin.y = 0.0f;

        _adBanner.frame = frame;

        [UIView commitAnimations];
    }
}

-(void)hideBannerView{
    if (_adBanner && _adBannerViewIsVisible){
        _adBannerViewIsVisible = false;

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        CGRect frame = _adBanner.frame;
        frame.origin.x = 0.0f;
        frame.origin.y = -_adBanner.frame.size.height ;

        _adBanner.frame = frame;

        [UIView commitAnimations];
    }

}

The only remaining thing I need to figure out is how to call these functions on the correct parent ViewController.

How can I go about doing this from within one of my SKScenes?

I've tried (from within the skscene):

[self.view.window.rootViewController hideBannerView];
[self.view.window.rootViewController showBannerView];

and

ViewController *parent = [[ViewController alloc] init];
[parent hideBannerView];

The first one just throws an error, and the parent code just doesn't do anything because it's creating another view controller rather than accessing the given one.

I also tried making a property on the skscene of type 'ViewController *' and it didn't let me access the property from the viewcontroller (I was trying to set the property equal to 'self', effectively referencing the viewcontroller from the skscene)

War es hilfreich?

Lösung

I'm not entirely familiar with spriteKit, but I think this should work just the same.

In RootViewController.h in viewDidLoad add:

// Note Notification name:  you should probably use a constant
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateBannerView:)
                                             name:@"kNotificationUpdateBannerView"
                                           object:nil];

Then, still in RootViewController.h add:

- (void) updateBannerView:(NSNotification *)note {
    NSDictionary * info = note.userInfo;
    BOOL shouldHide = [info[@"shouldHide"]boolValue];

    if (shouldHide) {
        NSLog(@"shouldHide");
        [self hideBannerView];
    }
    else {
        NSLog(@"shouldShow");
        [self showBannerView];
    }
}

Now, you're all set up. Whenever you want to call it, use this:

BOOL shouldHide = YES; // whether or not to hide

// Update Here!
NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithBool:shouldHide], @"shouldHide", nil];

[[NSNotificationCenter defaultCenter]postNotificationName:@"kNotificationUpdateBannerView" // Better as constant!
                                                   object:self 
                                                 userInfo:dict];

Now, wherever you are in your app, you can hide/show your banner ads!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top