你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS完成輪播圖banner示例

iOS完成輪播圖banner示例

編輯:IOS開發綜合

樓主項目中需求有一個輪播圖,由於比擬復雜,就自己寫了個,由於是從網上弄得圖片 所以用了SDWebImage 這個三方庫 當然自己也可以去掉

類型前面有*號 如用運用 請自行加上。。。。。

代碼:.h 文件

@protocol TJXViewDelegate<NSObject>
//判別點擊的那個
-(void)sendImageName:(TJXView *)TJXView andName:(NSInteger)selectImage;
@end
@interface TJXView : UIView
@property (nonatomic,weak)id<TJXViewDelegate>delegate;
//傳一個frame 和 裝有圖片名字的數組過去
//參數一:frame
//參數二:裝有圖片名字的數組
//參數三:BOOL假如是YES,那麼自動滾動,假如是NO不滾動
-(id)initWithFrame:(CGRect)frame andImageNameArray:
(NSMutableArray * )imageNameArray andIsRunning:(BOOL)isRunning;
@end

.m文件

@interface TJXView()<UIScrollViewDelegate>
{
  NSInteger _currentPage; //記載真實的頁碼數
  NSTimer *_timer; //生命一個全局變量
}
@property (nonatomic,assign) BOOL isRun;
@property (nonatomic,strong) NSMutableArray *imageArray;//存儲圖片的名字
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,assign) CGFloat width;//view的寬
@property (nonatomic,assign) CGFloat height;//view的高
@end

-(id)initWithFrame:(CGRect)frame andImageNameArray:(NSMutableArray *)imageNameArray andIsRunning:(BOOL)isRunning{
  self = [super initWithFrame:frame];
  if (self) {
    _width = self.frame.size.width;
    _height = self.frame.size.height;
    //arrayWithArray 把數組中的內容放到一個數組中前往
    self.imageArray = [NSMutableArray arrayWithArray:imageNameArray];
    //在數組的尾部添加原數組第一個元素
    [self.imageArray addObject:[imageNameArray firstObject]];
    //在數組的首部添加原數組最後一個元素
    [self.imageArray insertObject:[imageNameArray lastObject] atIndex:0];
    self.isRun = isRunning;
    _currentPage = 0;
    [self createSro];
    [self createPageControl];
    [self createTimer];
  }
  return self;
}
-(void)createTimer{
  if (_isRun == YES) {
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES ];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];  }
}
-(void)change{
  //1取得以後的點
  CGPoint point = _scrollView.contentOffset;
  //2求得將要變換的點
  CGPoint endPoint = CGPointMake(point.x+_width, 0);
  //判別
  if (endPoint.x == (self.imageArray.count-1)*_width) {
    [UIView animateWithDuration:0.25 animations:^{
      _scrollView.contentOffset = CGPointMake(endPoint.x, 0);
    } completion:^(BOOL finished) {
      //動畫完成的block
      _scrollView.contentOffset = CGPointMake(_width, 0);
      CGPoint realEnd = _scrollView.contentOffset;
      //取一遍頁碼數
      _currentPage = realEnd.x/_width;
      _pageControl.currentPage = _currentPage-1;
    }];
  }
  else{
    //0.25s中更改一個圖片
    [UIView animateWithDuration:0.25 animations:^{
      _scrollView.contentOffset = endPoint;
    } completion:^(BOOL finished) {
    }];
        CGPoint realEnd = _scrollView.contentOffset;
    //取一遍頁碼數
    _currentPage = realEnd.x/_width;
    _pageControl.currentPage = _currentPage-1;
  }  
}
//創立頁碼指示器
-(void)createPageControl{
  _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_width-200, _height-30, 100, 30)];
  _pageControl.centerX = _width/2;
  _pageControl.numberOfPages = self.imageArray.count-2;
  _pageControl.pageIndicatorTintColor = WP_GRAY_COLOR;
  _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
  _pageControl.userInteractionEnabled = NO;
  [self addSubview:_pageControl];
}
//創立滾動視圖
-(void)createSro{
  _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _width, _height)];
  _scrollView.contentSize = CGSizeMake(_width*self.imageArray.count, _height);
  for (int i = 0; i < self.imageArray.count; i++) {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*_width, 0, _width, _height)];
//    imageView.image = [UIImage imageNamed:self.imageArray[i]];
    [imageView sd_setImageWithURL:self.imageArray[i] placeholderImage:[UIImage imageNamed:@"home_banner_blank"]];
    imageView.userInteractionEnabled = YES;
    imageView.tag = 200+i;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithtarget:self action:@selector(tap:)];
    [imageView addGestureRecognizer:tap];
    [_scrollView addSubview:imageView];
  }
  //程度指示條不顯示
  _scrollView.showsHorizontalScrollIndicator = NO;
  //封閉彈簧效果
  _scrollView.bounces = NO;
  //設置用戶看到第一張
  _scrollView.contentOffset = CGPointMake(_width, 0);
  //設置代理
  _scrollView.delegate = self;
  //分頁效果
  _scrollView.pagingEnabled = YES;
  [self addSubview:_scrollView];
}
-(void)tap:(UITapGestureRecognizer *)tap{
  if(_delegate&&[_delegate respondsToSelector:@selector(sendImageName:andName:)]){
    [_delegate sendImageName:self andName:tap.view.tag-201];
  }else{
    NSLog(@"沒有設置代理或許沒有事前協議的辦法");
  } 
}
#pragma mark UIScrollViewDelegate
//中止滾動
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  if (_timer) {
    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
  }
  //圖片的個數 1 2 3 4 5 6 7 8
  //真實的頁碼 0 1 2 3 4 5 6 7
  //顯示的頁碼  0 1 2 3 4 5
  CGPoint point = _scrollView.contentOffset;
  if (point.x == (self.imageArray.count-1)*_width) {
    scrollView.contentOffset = CGPointMake(_width, 0);
  }
  if (point.x == 0) {
    scrollView.contentOffset = CGPointMake((self.imageArray.count-2)*_width, 0);
  }
  //取一遍頁碼數
  CGPoint endPoint = scrollView.contentOffset;
  _currentPage = endPoint.x/_width;
  _pageControl.currentPage = _currentPage-1;
}
//手指開端觸摸的時分,中止計時器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  if (_timer) {
    //假如有,停掉
    [_timer setFireDate:[NSDate distantFuture]];
  }
}

在項目中  導入頭文件  恪守代理

    TJXView * TJXView = [[TJXView alloc]initWithFrame:CGRectMake(0, 0, WPSCREEN_WIDTH, 100*WPSCREEN_HIGTH_RATIO) andImageNameArray:self.bannerImager andIsRunning:YES];
    TJXView.delegate = self;
    [self.view addSubview: TJXView];
#pragma mark TJXViewDelegate
-(void)sendImageName:(TJXView *) TJXView andName:(NSInteger)selectImage{
   KKLog(@"%ld",(long)selectImage);
}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持本站。

【iOS完成輪播圖banner示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved