自定义广告横幅

时间:2013-01-08 15:47:42

标签: iphone ios objective-c

我花了一段时间环顾四周,但似乎无法找到很多关于这个问题。我想制作像iAd一样的横幅广告,但是我自己的应用内广告。广告将使用户进入应用内的视图。

这是我展示广告横幅的设计。

Ad banner design

This is what I have managed to implement so far

到目前为止,我遇到了两个问题。

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");

}

这是我添加的视图,作为主视图的子视图。

任何帮助都会很精彩。 感谢

1 个答案:

答案 0 :(得分:1)

您是否希望将两个数组用于图像,另一个用于链接目标。

离。 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];
    }