星空网 > 软件开发 > 操作系统

UIscrollView和UIPageControl的循环滚动

 

因为昨天在网上找了很久,很多只能实现向右滚动,而且一张图一个imageview ,感觉工作量很可怕啊 ,  下面的例子就是不论你多少图 , 只和我代码里面的几个数值有关,  只需要修改分页和循环i的最大值,当然为了方便 , 您最好把图片的名字改成有序的 。 方便您添加到可变集合中。

 

 

 

 

 

 

 

UIscrollView和UIPageControl的循环滚动

 

 

如果这样的页面你有五页 或则更多都可以实现好像从最后一张图跳到第一张图。

这个其实总共只有3个image,图都是用循环加进去的 。

上代码

#import <UIKit/UIKit.h>#define WIDTH self.view.bounds.size.width#define HEIGHT self.view.bounds.size.height@interface ViewController : UIViewController<UIScrollViewDelegate>@property(strong,nonatomic) UIScrollView *scrollview;@property(strong,nonatomic) UIPageControl *pagecontrol;//存储图片@property(strong,nonatomic) NSMutableArray * imageArray;//当前页码@property(assign,nonatomic) int currentPage;//存储图片@property(strong,nonatomic) UIImageView * firstImage;@property(strong,nonatomic) UIImageView * secondImage;@property(strong,nonatomic) UIImageView *thirdImage;@end

 

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  self.scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, WIDTH,HEIGHT )];    self.scrollview.contentSize=CGSizeMake(WIDTH*3, 0);  //是否分页  self.scrollview.pagingEnabled=YES;  //添加代理  self.scrollview.delegate=self;  //隐藏滚动条  self.scrollview.showsHorizontalScrollIndicator=NO;  [self.view addSubview:self.scrollview];    self.pagecontrol=[[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH/5*3, HEIGHT/5*4, WIDTH/3, 40)];  //设置当前页  self.pagecontrol.currentPage=0;  //分页  self.pagecontrol.numberOfPages=5;  //指定页码颜色  self.pagecontrol.currentPageIndicatorTintColor=[UIColor redColor];  self.pagecontrol.pageIndicatorTintColor=[UIColor blueColor];  [self.view addSubview:self.pagecontrol];    //初始化存储图片的集合  self.imageArray=[NSMutableArray array];  for (int i=1; i<6; i++) {    UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];    [self.imageArray addObject:image];  }    self.firstImage=[[UIImageView alloc]init];  self.secondImage=[[UIImageView alloc]init];  self.thirdImage=[[UIImageView alloc]init];  //当前页码  self.currentPage=0;  [self reloadImage];          }-(void)reloadImage{  //第一种情况 , 第一页  if (self.currentPage==0) {    self.firstImage.image=[self.imageArray lastObject];    self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];    self.thirdImage.image = [self.imageArray objectAtIndex:self.currentPage + 1];  }  //  第二种情况 最后一页  else if (self.currentPage == self.imageArray.count - 1) {    self.firstImage.image = [self.imageArray objectAtIndex:self.currentPage - 1];    self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];    self.thirdImage.image = [self.imageArray objectAtIndex:0];  }  //  第三种情况 中间页  else {    self.firstImage.image = [self.imageArray objectAtIndex:self.currentPage - 1];    self.secondImage.image = [self.imageArray objectAtIndex:self.currentPage];    self.thirdImage.image = [self.imageArray objectAtIndex:self.currentPage + 1];  }  self.firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT);  self.secondImage.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);  self.thirdImage.frame = CGRectMake(WIDTH* 2, 0, WIDTH, HEIGHT);    [self.scrollview addSubview:self.firstImage];  [self.scrollview addSubview:self.secondImage];  [self.scrollview addSubview:self.thirdImage];    self.scrollview.contentOffset = CGPointMake(WIDTH, 0);    }-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    float x= self.scrollview.contentOffset.x;  //向左  if (x<0||x==0) {    if (self.currentPage==0) {      self.currentPage=(int)self.imageArray.count-1;    }    else{      self.currentPage--;    }  }  //向右  if (x > WIDTH * 2 || x == WIDTH * 2) {    if (self.currentPage == (int)self.imageArray.count - 1) {      self.currentPage = 0;    }    else {      self.currentPage++ ;    }  }    self.pagecontrol.currentPage = self.currentPage;    [self reloadImage];    }- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}@end

 

 

UIscrollView和UIPageControl的循环滚动




原标题:UIscrollView和UIPageControl的循环滚动

关键词:ip

ip
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

高纬度的云:https://www.goluckyvip.com/tag/31626.html
高先生:https://www.goluckyvip.com/tag/31627.html
高翔物流:https://www.goluckyvip.com/tag/31628.html
高效物流:https://www.goluckyvip.com/tag/31629.html
Yura zhon:https://www.goluckyvip.com/tag/3163.html
高邮跨境电子商务协会:https://www.goluckyvip.com/tag/31631.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
如何用摄影作品表现“芳草鲜美,落英缤纷”的:https://www.vstour.cn/a/411247.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流