设为首页 加入收藏

TOP

UIscrollView和UIPageControl的循环滚动(一)
2017-10-12 18:17:09 】 浏览:2843
Tags: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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇the bundle at bundle path is no.. 下一篇1014-32-首页13-cell的结构分析--..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目