UIscrollView和UIPageControl的循环滚动

 

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

 

 

 

 

 

 

 

 

 

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

这个其实总共只有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

 

 

posted on 2016-03-12 11:25  无梦为安  阅读(539)  评论(0编辑  收藏  举报

导航