3

私はしばらく周りを見回してきましたが、この件についてはあまり見つけられないようです。iAd のようなバナーを自分のアプリ内広告用に作成したいと考えています。広告により、ユーザーはアプリ内のビューに移動します。

これは、広告バナーを表示するために作成したデザインです。

広告バナーのデザイン

これは私がこれまでに実装したものです。

私がこれまでやってきたことには2つの問題があります。

1) 画像の順番をカスタマイズしたい。

メイン広告とサブ広告を希望します。メイン広告が最初に再生され、他のすべての画像がメイン広告になります。次に、サブ広告はランダムな順序にする必要があります。どうすればこれを達成できますか?現在のアニメーションの方法でそれを行うことができますか、それともより良い解決策がありますか?

2) 画像を別の場所にリンクする方法がわかりません。

これまでのところ、すべての画像が同じアクションを実行するようになっています。それらを別のビューに変更するにはどうすればよいですか?

これは私がこれまでに持っているものです。

@implementation SponsorBannerView{

}


@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{

    //get the sponsors banners and id's. 
    DataBase * dataBase = [[DataBase alloc] init];
    [dataBase openDB];
    NSMutableDictionary *BannersAndId = [dataBase getBanners];
    NSLog(@"banner and id's : %@",BannersAndId);

    //create an id array and a banner array.
    self.poiIDs = [BannersAndId allKeys];
    self.banners = [BannersAndId allValues];

    //get the root file path for the images 
    NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];

    //create an array to hold the image 
    NSMutableArray * images = [[NSMutableArray alloc]init];

    //create an array containing all the images
    for (int i = 0; i<self.banners.count; i++) {
        NSString *tmp = [NSString stringWithFormat:@"%@/%@",documentsDirectory, self.banners[i]];
        UIImage * tmpIamge = [UIImage imageWithContentsOfFile:tmp];
        [images addObject:tmpIamge];
    }


    //cast the mutable array into an array
    NSArray *array = [NSArray arrayWithArray:images];

    //set all the banner settings 
    bannerImage=[[UIImageView alloc]init];
    bannerImage.frame=CGRectMake(0, 0, 320, 50);
    bannerImage.backgroundColor=[UIColor purpleColor];
    [bannerImage setAnimationImages:array];
    [bannerImage setAnimationDuration:6];
    [bannerImage setAnimationRepeatCount:0];
    [bannerImage startAnimating];


    //add to view. 
    [self addSubview:bannerImage];

    // add a uibutton on top of the uiimageview and assign an action for it
    UIButton* actionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    actionButton.frame=CGRectMake(0, 0, 320, 50);
    [actionButton addTarget:self action:@selector(sponsorBannerAction) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:actionButton];
}

-(void) sponsorBannerAction{
    // do what ever here like going to an other uiviewController as you mentionned
    NSLog(@"Pressed");

}

これは、メイン ビューのサブ ビューとして追加したビューです。

どんな助けでも素晴らしいでしょう。ありがとう

4

1 に答える 1

1

1 つは画像用、もう 1 つはリンク ターゲット用の 2 つの配列が必要ですか。

元。img1、img2、img3 および img1ClickedMethod、img2ClickedMethod、img3ClickedMethod

ボタンの画像を交換するときは、ボタン クリックのターゲットも新しいメソッドに変更します。

すなわち[actionButton addTarget:self action:@selector(img1ClickedMethod) forControlEvents:UIControlEventTouchUpInside];[actionButton addTarget:self action:@selector(img2ClickedMethod) forControlEvents:UIControlEventTouchUpInside];

    - (void)nextImageFade{

        NSArray *imgArray;
imgArray = [NSArray arrayWithObjects:
@"img1.jpg",
@"img2.jpg",
@"img3.jpg",
  nil]; 
 NSArray *methodArray;
imgArray = [NSArray arrayWithObjects:
@"clicked1",
@"clicked2",
@"clicked2",
  nil];   
        UIImage *image1 = [UIImage imageNamed:[imageArray objectAtIndex:currentImg]];

        if(currentImg < [imgs count]-1){
            currentImg++;
        }else{
            currentImg = 0;
        }
        UIImage *image2 = [UIImage imageNamed:[imgArray objectAtIndex:currentImg]];


    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
        crossFade.delegate = self;
        crossFade.duration = 0.5;
        crossFade.fromValue = (id)image1.CGImage;
        crossFade.toValue = (id)image2.CGImage;
        [_imgView1.layer addAnimation:crossFade forKey:@"animateContents"];
        _imgView1.image = image2;
//This this may work for changing targets
        [actionButton addTarget:self action:@selector([methodArray objectAtIndex:CurrentImg]) forControlEvents:UIControlEventTouchUpInside];

        if(nextImgTimer != nil){
            [nextImgTimer invalidate];
        }

        nextImgTimer = [NSTimer scheduledTimerWithTimeInterval:5 
                                                        target:self 
                                                      selector:@selector(nextImageFade) 
                                                      userInfo:nil 
                                                       repeats:NO];
    }
于 2013-01-09T15:02:08.387 に答える