强制 ADBannerView 旋转(不是“方向"而是实际变换) [英] Force ADBannerView to rotate (Not "orientation" but actual transform)

查看:60
本文介绍了强制 ADBannerView 旋转(不是“方向"而是实际变换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是我如何强制我的应用程序问题的方向,因为它看起来像.

this is NOT a how do I force orientation of my app question as it might look like.

我的问题可能很简单,但很难描述,所以就这样吧.

My problem is probably very simple, but it is hard to describe it so here goes.

我正在尝试在我的 iphone 游戏中实现 iAd.这不是问题,只需 10 分钟即可修复,只需按照网络上的教程进行操作即可.但是,我的游戏是由承包商编写的,因为我不能很好地编程,他将游戏转换为横向而不是横向.在尝试正确轮播广告时,这给我带来了一些问题.

I am trying to implement iAd to my iphone game. This is not a problem, it was a 10 minute fix, just follow tutorials on the web. However, my game was programmed by a contractor since I can't program very well and he made the game translated to landscape orientation instead of oriented to landscape. This has leads to some problems for me when trying to rotate the ads correctly.

最重要的是,iAd 使用的 CGRect 根本没有转换功能,所以无论我做什么,广告都站在他们一边.这是很自然的,因为应用程序不知道游戏是要在横向上玩的.

Bottom line is that CGRect which is what iAd uses does simply not have the transform function so no matter what I do the ads are standing on their side. This is quite natural since the app doesn't know that the game is meant to be played in landscape.

经过一整天的研究,似乎我需要将 iAd CGRect 放在不同的视图中,并使用 CGAffineTransformMakeRotation 旋转该视图.我真正的大问题是我的 Objective-C 不够好,无法真正做到这一点.

After a full day of research it seems that I need to put my iAd CGRect in a different view and rotate that view using the CGAffineTransformMakeRotation. My really big problem is that I am not good enough at Objective-C to actually do that.

那么你能帮助我如何将转换应用到我的广告中吗?

So can you help me how I should be able to apply transform to my ad?

下面的代码编译并显示广告,但站在它的一边(当横向拿着游戏时)

Code below compiles and shows the ad but standing on its side (when holding the game in landscape)

//iAD starts
// lower right:-136, 295, 320, 32    lower left:-136, 135, 320, 32   upper right:136, 295, 320, 32
// *Controller becomes a UIViewController
UIViewController *controller = [[UIViewController alloc] init];
controller.view.frame = CGRectMake(100, 100, 320, 32);
//controller.view.transform = CGAffineTransformMakeRotation(M_PI / 2.0); // turn 180 degrees
NSLog(@"*controller");  


    //adView becomes a CGRectZero called adView
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
//adView.frame = CGRectOffset(adView.frame, 0, 0);
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierLandscape,ADBannerContentSizeIdentifierPortrait,nil];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
[self.view addSubview:adView];


adView.delegate=self;

//self.bannerIsVisible=NO;

// iAD ends

最好的问候马库斯

推荐答案

这应该按照您的要求轮换.此代码在我的 iAd 应用程序中对我有用:直接来自 Apple 源代码

This should rotate as you are asking. This code has worked for me in my iAd apps: Directly from Apple Source Code

.h

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface TextViewController : UIViewController <ADBannerViewDelegate>
{
    UIView *contentView;
    ADBannerView *banner;
}

@property(nonatomic, retain) IBOutlet UIView *contentView;
@property(nonatomic, retain) IBOutlet ADBannerView *banner;

@end

.m

#import ".h"

@interface TextViewController()

// Layout the Ad Banner and Content View to match the current orientation.
// The ADBannerView always animates its changes, so generally you should
// pass YES for animated, but it makes sense to pass NO in certain circumstances
// such as inside of -viewDidLoad.
-(void)layoutForCurrentOrientation:(BOOL)animated;

// A simple method that creates an ADBannerView
// Useful if you need to create the banner view in code
// such as when designing a universal binary for iPad
-(void)createADBannerView;

@end

@implementation TextViewController

@synthesize contentView, banner;

-(void)viewDidLoad{
    [super viewDidLoad];
    // If the banner wasn't included in the nib, create one.
    if(banner == nil)
    {
        [self createADBannerView];
    }
    [self layoutForCurrentOrientation:NO];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutForCurrentOrientation:NO];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES;
}

-(void)willAnimateRotationToInterfaceOrientation:  (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [self layoutForCurrentOrientation:YES];
}

-(void)createADBannerView{
    // --- WARNING ---
    // If you are planning on creating banner views at runtime in order to support iOS targets that don't support the iAd framework
    // then you will need to modify this method to do runtime checks for the symbols provided by the iAd framework
    // and you will need to weaklink iAd.framework in your project's target settings.
    // See the iPad Programming Guide, Creating a Universal Application for more information.
    // http://developer.apple.com/iphone/library/documentation/general/conceptual/iPadProgrammingGuide/Introduction/Introduction.html
    // --- WARNING ---

    // Depending on our orientation when this method is called, we set our initial content size.
    // If you only support portrait or landscape orientations, then you can remove this check and
    // select either ADBannerContentSizeIdentifierPortrait (if portrait only) or ADBannerContentSizeIdentifierLandscape (if landscape only).
    NSString *contentSize;
    if (&ADBannerContentSizeIdentifierPortrait != nil)
    {
        contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
    }
    else
    {
        // user the older sizes 
        contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32;
    }

    // Calculate the intial location for the banner.
    // We want this banner to be at the bottom of the view controller, but placed
    // offscreen to ensure that the user won't see the banner until its ready.
    // We'll be informed when we have an ad to show because -bannerViewDidLoadAd: will be called.
    CGRect frame;
    frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
    frame.origin = CGPointMake(0.0f, CGRectGetMaxY(self.view.bounds));

    // Now to create and configure the banner view
    ADBannerView *bannerView = [[ADBannerView alloc] initWithFrame:frame];
    // Set the delegate to self, so that we are notified of ad responses.
    bannerView.delegate = self;
    // Set the autoresizing mask so that the banner is pinned to the bottom
    bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
    // Since we support all orientations in this view controller, support portrait and landscape content sizes.
    // If you only supported landscape or portrait, you could remove the other from this set.

    bannerView.requiredContentSizeIdentifiers = (&ADBannerContentSizeIdentifierPortrait != nil) ?
        [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] : 
        [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];

    // At this point the ad banner is now be visible and looking for an ad.
    [self.view addSubview:bannerView];
    self.banner = bannerView;
    [bannerView release];
}

-(void)layoutForCurrentOrientation:(BOOL)animated{
    CGFloat animationDuration = animated ? 0.2f : 0.0f;
    // by default content consumes the entire view area
    CGRect contentFrame = self.view.bounds;
    // the banner still needs to be adjusted further, but this is a reasonable starting point
    // the y value will need to be adjusted by the banner height to get the final position
    CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame),CGRectGetMaxY(contentFrame));
    CGFloat bannerHeight = 0.0f;

    // First, setup the banner's content size and adjustment based on the current orientation
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
        banner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
    else
        banner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50; 
    bannerHeight = banner.bounds.size.height; 

    // Depending on if the banner has been loaded, we adjust the content frame and banner location
    // to accomodate the ad being on or off screen.
    // This layout is for an ad at the bottom of the view.
    if(banner.bannerLoaded)
    {
        contentFrame.size.height -= bannerHeight;
        bannerOrigin.y -= bannerHeight;
    }
    else
    {
        bannerOrigin.y += bannerHeight;
    }

    // And finally animate the changes, running layout for the content view if required.
    [UIView animateWithDuration:animationDuration
                 animations:^{
                     contentView.frame = contentFrame;
                     [contentView layoutIfNeeded];
                     banner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, banner.frame.size.width, banner.frame.size.height);
                 }];
}

-(void)viewDidUnload{
    self.contentView = nil;
    banner.delegate = nil;
    self.banner = nil;
}

-(void)dealloc{
    [contentView release]; contentView = nil;
    banner.delegate = nil;
    [banner release]; banner = nil; 
    [super dealloc];
}

#pragma mark ADBannerViewDelegate methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
    [self layoutForCurrentOrientation:YES];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
    [self layoutForCurrentOrientation:YES];
}

-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave{
    return YES;
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
}

@end

这篇关于强制 ADBannerView 旋转(不是“方向"而是实际变换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆