温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS如何实现轮播图banner

发布时间:2021-06-30 12:46:32 来源:亿速云 阅读:217 作者:小新 栏目:移动开发

小编给大家分享一下iOS如何实现轮播图banner,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片 所以用了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”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI