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

iOS UIScrollView滚动视图/无限循环滚动/自动滚动的实例代码

程序员文章站 2024-02-15 20:09:10
我们都知道uiscrollview有一种很流畅的切换效果,结合uipagecontrol的辅助展示效果,就可以完成一个很不错的产品介绍功能页面。下面给大家分享ios uis...

我们都知道uiscrollview有一种很流畅的切换效果,结合uipagecontrol的辅助展示效果,就可以完成一个很不错的产品介绍功能页面。下面给大家分享ios uiscrollview滚动视图/无限循环滚动/自动滚动功能,具体代码如下所示;

<uiscrollviewdelegate>
#define width [[uiscreen mainscreen] bounds].size.width
#define height [[uiscreen mainscreen] bounds].size.height
@property (nonatomic, strong)nstimer *timer; //定时器
@property (nonatomic, retain)nsmutablearray *arr; //放图片的数组
@property (nonatomic, retain)uiview *headerview; //tableview的表头
@property (nonatomic, retain)uiimageview *image; //图片
@property (nonatomic, retain)uiscrollview *scrollview; 
@property (nonatomic, retain)uipagecontrol *pagec; //页码
//设置头视图
- (void)headimage{
  //图片数组
  self.arr = [nsmutablearray arraywithobjects:@"8.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"1.jpg", nil];
  self.scrollview = [[uiscrollview alloc]initwithframe:cgrectmake(0, 0, width, 200 * height/667)];
  self.scrollview.backgroundcolor = [uicolor clearcolor];
  //设置滚动量
  self.scrollview.contentsize = cgsizemake(width * self.arr.count, 0);
  //设置偏移量
  self.scrollview.contentoffset = cgpointmake(width, 0);
  //设置按页滚动
  self.scrollview.pagingenabled = yes;
  //设置是否显示水平滑动条
  self.scrollview.showshorizontalscrollindicator = no;
  //设置是否边界反弹
  self.scrollview.bounces = no;
  //把scrollview添加到tableview的表头的视图上
  [self.headerview addsubview:self.scrollview];
  [_scrollview release];
  //循环图片添加到uiimageview上
  for (int i = 0 ; i < self.arr.count; i++) {
    nsstring *name = [self.arr objectatindex:i];
    uiimage *img = [uiimage imagenamed:name];
    self.image = [[uiimageview alloc]init];
    self.image.frame = cgrectmake(i * width, 0, width, 200 * height/667);
    self.image.image = img;
    [self.scrollview addsubview:self.image];
    [_image release];
  }
  self.scrollview.delegate = self;
  //设置页面
  self.pagec = [[uipagecontrol alloc]initwithframe:cgrectmake(100 * width/375, 120 * height/667, 200* width/375, 60*height/667)];
  self.pagec.backgroundcolor = [uicolor clearcolor];
  //把页码添加到头视图上
  [self.headerview addsubview:self.pagec];
  //设置页码数
  self.pagec.numberofpages = self.arr.count;
  //设置选中页码的颜色
  self.pagec.currentpageindicatortintcolor = [uicolor browncolor];
  //设置未选中的页码颜色
  self.pagec.pageindicatortintcolor = [uicolor graycolor];
  //设置当前选中页
  self.pagec.currentpage = 0;
  //核心方法
  [self.pagec addtarget:self action:@selector(pageaction:) forcontrolevents:uicontroleventvaluechanged];
  [_pagec release];
  //自定义一个定时器方法
  [self addtimer];
}
 //定时器执行方法
- (void)change:(nstimer *)time{
  if (self.pagec.currentpage == self.pagec.numberofpages - 1) {
    self.pagec.currentpage = 0;
  } else if (self.pagec.currentpage < self.pagec.numberofpages - 1) {
    self.pagec.currentpage++;
  }
  [self.scrollview setcontentoffset:cgpointmake((self.pagec.currentpage + 1) * width, 0) animated:no];
 }

以上所述是小编给大家介绍的ios uiscrollview滚动视图/无限循环滚动/自动滚动的实例代码,希望对大家有所帮助