您的位置:首页 > 产品设计 > UI/UE

IOS开发——UIPageControl页面翻页循环

2014-03-11 20:59 246 查看
IOS开发——UIPageControl页面翻页循环

原理:比如桌面上从左到右,有一排10张图片,我们可以把第十张图片复制一份放到第1张图片之前,同时把第一张图片复制一份放置第10张图片之后。那么现在我们就有了12张图片,多添加的两张图片用作移形换位之用,让视觉上只能感受10张图片。

笔记之用,只有截取取一段代码:

-(void)gmSectionZeroRowZero:(NSArray *)theImgArr andHight:(float)theHight{
//在传回的图片数组的对象个数基础上+2
self.ymSVCycle = [theImgArr count]+2;
//为了实现循环,ScrollView 的宽度也要多两个单位
self.ymSoRoIV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, theHight)];
[self.ymSoRoIV setContentSize:CGSizeMake(320*self.ymSVCycle,theHight )];
//设置ScrollView 的初始偏移
[self.ymSoRoIV setContentOffset:CGPointMake(320, 0)];
[self.ymSoRoIV setBounces:NO];
[self.ymSoRoIV setPagingEnabled:YES];
[self.view addSubview:self.ymSoRoIV];

//设置代理
[self.ymSoRoIV setDelegate:self];
[self.ymCellView setFrame:CGRectMake(0, 0, 320, theHight)];
for(int i = 0;i < self.ymSVCycle ; i ++){
UIImageView *tempImg = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, theHight)];
//索引为0时,赋最后一张图片
if(i == 0){
[tempImg setImage:[theImgArrobjectAtIndex:self.ymSVCycle-3]];
//索引为最后一个时,赋第一张图片
}else if(i == self.ymSVCycle-1){
[tempImg setImage:[theImgArr objectAtIndex:0]];
}else{
[tempImg setImage:[theImgArr objectAtIndex:i-1]];
}
[self.ymSoRoIV addSubview:tempImg];
}
//UIPageControl
self.ymPVCFirst = [[UIPageControl alloc] init];
[self.ymPVCFirst setFrame:CGRectMake(350 - self.ymSVCycle*15, theHight-15, self.ymSVCycle*15,10)];
self.ymPVCFirst.numberOfPages = self.ymSVCycle-2;//总共多少页
self.ymPVCFirst.currentPage = 0;//默认选中第几页
[self.view addSubview:self.ymPVCFirst ];
[self.ymPVCFirst setDefersCurrentPageDisplay:YES];
//  self.ymPVCFirst.userInteractionEnabled=NO;
}

//添加委托
[self.ymPVCFirst addTarget:selfaction:@selector(gmPVCFristAction)forControlEvents:UIControlEventTouchDown];
}

#pragma mark UIScrollViewDelegate
// 任何偏移改变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint thePoint = [scrollView contentOffset];
if(thePoint.x / 320 == 0){
self.ymPVCFirst.currentPage = self.ymSVCycle - 2;
}
else if (thePoint.x / 320 == self.ymSVCycle - 1) {
self.ymPVCFirst.currentPage = 0;
}else{
self.ymPVCFirst.currentPage = thePoint.x / 320 - 1;
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
CGPoint thePoint = [scrollView contentOffset];
if(thePoint.x/320 == self.ymSVCycle-1){
scrollView.contentOffset = CGPointMake(320, 0);
self.ymPVCFirst.currentPage = 0;
}else if(thePoint.x/320 == 0){
scrollView.contentOffset =CGPointMake((self.ymSVCycle-2)*320, 0);
self.ymPVCFirst.currentPage = self.ymSVCycle-2;
}
}

//UIPageControl关联方法
-(void)gmPVCFristAction{
int page = self.ymPVCFirst.currentPage;
[self.ymSoRoIVsetContentOffset:CGPointMake(320*page+320, 0)];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: