欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS实现轮播图banner示例

时间:2020-11-25 11:53:56|栏目: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开发实现HTTPS之cer文件的使用详解

栏    目:iOS代码

下一篇:iOS开发中Quartz2D的基本使用方式举例

本文标题:iOS实现轮播图banner示例

本文地址:http://www.codeinn.net/misctech/26061.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有