Will
Will

Reputation: 3044

Custom Advert banner

I have spent a while looking around but cant seem to find much on the subject. I would like to make a banner like iAd's but for my own in-app adverts. The adverts will take the user to a view within the app.

Here is a design that I have made to show the ad banners.

Ad banner design

This is what I have managed to implement so far.

I have two problems with what I have done so far.

1) I would like to customise the order of the images.

I would like a main ad then sub ad's. The main add should play first and every other image should be the main ad. Then the sub ad's should be in a random order. How could I achieve this? Could I do it with the current way im animation it or would there be a better solution?

2) I do not know how to link the images to different places.

So far I have it so all the images run the same action. How can I make them change to different views?

This Is what I have so far .

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

}

This is a view that I have added as a sub view of my main view.

Any help would be wonderful. Thanks

Upvotes: 3

Views: 529

Answers (1)

propstm
propstm

Reputation: 3629

Will you will want to have two arrays one for images, the other for link targets.

ex. img1, img2, img3 and img1ClickedMethod, img2ClickedMethod, img3ClickedMethod

When you swap your images in your button, also change the target of the button clicks to the new method.

i.e. [actionButton addTarget:self action:@selector(img1ClickedMethod) forControlEvents:UIControlEventTouchUpInside]; changes to [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];
    }

Upvotes: 1

Related Questions