Advertisement
wkerswell

asd

Jan 10th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  SponsorBannerView.m
  3. //  CPOP Test 4
  4. //
  5. //  Created by Will Kerswell on 07/01/2013.
  6. //  Copyright (c) 2013 Will Kerswell. All rights reserved.
  7. //
  8.  
  9. #import "SponsorBannerView.h"
  10. #import <QuartzCore/QuartzCore.h>
  11.  
  12. @implementation SponsorBannerView{
  13.     int currentImage;
  14.     NSTimer *nextImgTimer;
  15. }
  16.  
  17.  
  18. @synthesize bannerImage;
  19.  
  20. - (id)initWithFrame:(CGRect)frame{
  21.     self = [super initWithFrame:frame];
  22.     if (self) {
  23.         // Initialization code
  24.     }
  25.     return self;
  26. }
  27.  
  28. - (void)drawRect:(CGRect)rect{
  29.    
  30.     //get the sponsors banners and id's.
  31.     DataBase * dataBase = [[DataBase alloc] init];
  32.     [dataBase openDB];
  33.     NSMutableDictionary *BannersAndId = [dataBase getBanners];
  34.    
  35.     //create an id array and a banner array.
  36.     self.poiIDs = [BannersAndId allKeys];
  37.     self.banners = [BannersAndId allValues];
  38.    
  39.     //get the root file path for the images
  40.     NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  41.     NSString  *documentsDirectory = [paths objectAtIndex:0];
  42.  
  43.     //create an array to hold the image
  44.     NSMutableArray * images = [[NSMutableArray alloc]init];
  45.    
  46.     //create an array containing all the images
  47.     for (int i = 0; i<self.banners.count; i++) {
  48.         NSString *tmp = [NSString stringWithFormat:@"%@/%@",documentsDirectory, self.banners[i]];
  49.         UIImage * tmpIamge = [UIImage imageWithContentsOfFile:tmp];
  50.         [images addObject:tmpIamge];
  51.     }
  52.    
  53.  
  54.     //cast the mutable array into an array
  55.     self.banners = [NSArray arrayWithArray:images];
  56.    
  57.     [self nextImageFade];
  58.    
  59.  
  60.     //set all the banner settings
  61.     bannerImage=[[UIImageView alloc]init];
  62.     bannerImage.frame=CGRectMake(0, 0, 320, 50);
  63.     [bannerImage setAnimationImages:self.banners];
  64.     [bannerImage setAnimationDuration:8];
  65.     [bannerImage setAnimationRepeatCount:0];
  66.     [bannerImage startAnimating];
  67.    
  68.    
  69.    
  70.    
  71.     //add to view.
  72.     [self addSubview:bannerImage];
  73.    
  74.     // add a uibutton on top of the uiimageview and assign an action for it
  75.     UIButton* actionButton=[UIButton buttonWithType:UIButtonTypeCustom];
  76.     actionButton.frame=CGRectMake(0, 0, 320, 50);
  77.     [actionButton addTarget:self action:@selector(sponsorBannerAction) forControlEvents:UIControlEventTouchUpInside];
  78.    
  79.     [self addSubview:actionButton];
  80. }
  81.  
  82. -(void) sponsorBannerAction{
  83.     // do what ever here like going to an other uiviewController as you mentionned
  84.     NSLog(@"Pressed");
  85.  
  86. }
  87.  
  88. - (void)nextImageFade{
  89.    
  90.    
  91.     UIImage *image1 = [UIImage imageNamed:[self.banners objectAtIndex:currentImage]];
  92.    
  93.     if(currentImage < [self.banners count]-1){
  94.         currentImage++;
  95.     }else{
  96.         currentImage = 0;
  97.     }
  98.    
  99.     UIImage *image2 = [UIImage imageNamed:[self.banners objectAtIndex:currentImage]];
  100.    
  101.    
  102.     CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
  103.     crossFade.delegate = self;
  104.     crossFade.duration = 0.5;
  105.     crossFade.fromValue = (id)image1.CGImage;
  106.     crossFade.toValue = (id)image2.CGImage;
  107.     [_imgView1.layer addAnimation:crossFade forKey:@"animateContents"];
  108.     _imgView1.image = image2;
  109.     //This this may work for changing targets
  110.     [actionButton addTarget:self action:@selector([methodArray objectAtIndex:CurrentImg]) forControlEvents:UIControlEventTouchUpInside];
  111.    
  112.     if(nextImgTimer != nil){
  113.         [nextImgTimer invalidate];
  114.     }
  115.    
  116.     nextImgTimer = [NSTimer scheduledTimerWithTimeInterval:5
  117.                                                     target:self
  118.                                                   selector:@selector(nextImageFade)
  119.                                                   userInfo:nil
  120.                                                    repeats:NO];
  121. }
  122.  
  123.  
  124. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement