【问题标题】:Force ADBannerView to rotate (Not "orientation" but actual transform)强制 ADBannerView 旋转(不是“方向”而是实际变换)
【发布时间】:2011-09-11 20:18:41
【问题描述】:

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

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

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

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

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

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

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

//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

最好的问候 马库斯

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

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

    .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
    

    【讨论】:

    • 太棒了!如果/当我再次集成 iads 时,我将确保使用它。现在我选择了 mobfox,这对我来说更容易集成。非常感谢您的帮助!
    【解决方案2】:

    如果游戏轮换,为什么不停止游戏轮换?

    【讨论】:

    • 你不明白,资产和所有东西都是横着做的,这样当你在横向使用设备时它就变得正确了。该代码永远不会告诉设备它处于横向状态。严格来说,游戏是向右运行的,但是当你玩它时,你会像涂鸦跳跃一样向上玩,因为你是在横向玩的。这都是假的,这就是为什么我有这么多问题。
    • 所以告诉你的视图控制器你的横向,这样 ADBannerView 会正确显示吗?还是我不理解。如果您需要更多帮助,请将您的代码邮寄给我:benjaminzamayo@gmail.com
    • 我很乐意将代码邮寄给您,但是您需要什么?这是一个包含大量图像和声音的相当大的 xcode 项目,我可以只发送 viewController.m 和 .h 吗?最好的问候
    • 您使用的是 xib 吗?如果是这样也发送。
    • 非常感谢您的报价!我设法进行了转换,但现在我遇到了一个不同的(甚至更简单!)问题。我会把它贴在这里并链接到它。干杯!
    【解决方案3】:

    我设法通过使用不同的广告 SDK (mobfox) 来做我想做的事情,我想做的轮播如下所示:

    // MOBFOX Starts
    // create the banner view just outside of the visible area
    MobFoxBannerView *bannerView = [[MobFoxBannerView alloc] initWithFrame:
                                    CGRectMake(-800, self.view.bounds.size.height - 240, 320, 50)];
    bannerView.delegate = self;  // triggers ad loading
    //bannerView.backgroundColor = [UIColor darkGrayColor]; // fill horizontally
    bannerView.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 
    //bannerView.refreshAnimation = UIViewAnimationTransitionCurlDown;
    [self.view addSubview:bannerView];
    
    NSLog(@"MobFox: Ad initated and placed offscreen");
    
    //
    

    iAd 不接受 transform = CGAffineTransformMakeRotation,因为我在 Objective-c 上太弱了,无法按照我的意愿强制它。这就是我所做的。

    感谢您提供帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多