3

我花了一段时间环顾四周,但似乎找不到太多关于这个主题的东西。我想制作一个像 iAd 一样的横幅,但用于我自己的应用内广告。广告会将用户带到应用程序内的视图。

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

广告横幅设计

这是我迄今为止设法实现的

到目前为止,我所做的事情有两个问题。

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

您是否需要两个数组,一个用于图像,另一个用于链接目标。

前任。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 回答