欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现轮播图banner示例

程序员文章站 2024-02-16 22:09:10
楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片 所以用了sdwebimage 这个三方库 当然自己也可以去掉 类型后面有*号 如用使用 请自...

楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片 所以用了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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。