1

アプリで広告バナーを作成したいと考えています。少し iAd に似ています。

ビューに UIImage を配置し、バナー画像を割り当てることで作成するつもりでした。次に、タッチ ジェスチャを追加して、ユーザーがクリックしてアプリの別のビューに移動できるようにします。1 つのビューでこれを非常に簡単に実行できることはわかっていますが、アプリのほとんどのビューでこれを実行したいと考えています。同じコードを何度も書かずに複数のビューにバナーを追加する最良の方法は何ですか?

以下のデザインは、その後のバナーの種類を示しています。

ありがとう

スポンサーバナー

4

5 に答える 5

4
#import <UIKit/UIKit.h>
@class custom;

@protocol adDelegate
- (void)viewAd:(NSString *)adRate;
@end


@interface custom : UIView


@property (strong, nonatomic) UIImage *viewImage;
@property (assign) id <adDelegate> delegate;

@end

// メインクラス

#import "custom.h"

@implementation custom

@synthesize viewImage;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.image = viewImage;
        [self addSubview:imageView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder 
{
    if ((self = [super initWithCoder:aDecoder])) 
    {

    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

    [self.delegate viewAd:@"view"];
}
于 2013-01-07T14:32:37.300 に答える
1

いくつかの提案:

まず、ジェスチャでのUIButton代わりにを使用してみませんか?UIImage結局のところ、実際に行っているのはボタンの機能を複製することだけです...

次に、次のようにUIButtonを含むクラスを作成することで、おそらく全体的な問題に取り組みます。

@interface YourSuperViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIButton *adButton;

- (IBAction)adTouched:(id)sender;

@end

このviewDidLoadクラスので、ボタンを作成してビューに追加し、広告固有のロジックをadTouchedアクションに追加します。

次に、アプリの残りのビューをのインスタンスとして作成しますYourSuperViewController。そのようです:

@interface SomeOtherViewController : YourSuperViewController

これで、SomeOtherViewController自動的に魔法のように広告ボタンが表示され、タッチに適切に反応します。終わり!

于 2013-01-07T14:16:14.137 に答える
1

他の誰もが言っていることが最善の方法です。カスタム機能が必要な場合は、おそらくサブクラス化が最適です。

私はちょうど1つの衒学的なことを追加したかった. UIImage はビューではないことを覚えておくことが重要です。画面に UIImage が表示されたことはありません。UIImage はモデル オブジェクトです。それは単なるデータの集まりです。UIImageView はビュー オブジェクトであるため、UIImageView 自体を画面に表示できます。

これは過度に衒学的でつまらないように思えるかもしれませんが、MVC (モデル、ビュー、コントローラー) を効果的に使用するために、頭の中で整理しておくことが重要です。

于 2013-01-07T15:52:13.550 に答える
1

たとえば、UIView クラスを作成して、それを BannerView と呼ぶことができます。// bannerView.h 内

    #import <UIKit/UIKit.h>
@interface BannerView : UIView{ 
    UIImageView* bannerImage;
}
@property(nonatomic,retain) UIImageView* bannerImage;    
@end

//bannerView.m 内

#import "BannerView.h"

@implementation BannerView
@synthesize bannerImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    bannerImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"banner-image.png"]];
    bannerImage.frame=CGRectMake(0, 0, 320, 100);
    [self addSubview:bannerImage];

// add a uibutton on top of the uiimageview and assign an action for it 
// better than creating an action recogniser

    UIButton* actionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    actionButton.frame=CGRectMake(0, 0, 320, 100);
    [actionButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:actionButton];
}

-(void) yourAction{
// do what ever here like going to an other uiviewController as you mentionned 

}

@end

これで、この方法で任意のビュー コントローラーからこのビューを呼び出すことができます

 BannerView* banner=[[BannerView alloc] initWithFrame:CGRectMake(0, 300, 320, 100)];
    [self.view addSubview:banner];
于 2013-01-07T14:22:21.210 に答える
1

UIImageView とジェスチャ認識機能を使用して、バナーのすべての表示と処理を行う UIView から親クラスを作成してみてください。次に、この機能を必要とするビューがあれば、この親クラスから派生させ、メソッドでデフォルトの処理をオーバーライドして、子クラスの動作をカスタマイズできるようにします。

于 2013-01-07T14:10:17.127 に答える