设为首页 加入收藏

TOP

iOS-UIScrollView和UIPageControl的综合实力,滚动图,轮播图(一)
2017-10-12 18:17:05 】 浏览:4709
Tags:iOS-UIScrollView UIPageControl 综合 实力 滚动

本代码主要实现图片之间的切换


 

目录结构

 

代码

ViewController.m文件


#import "ViewController.h"

@interface ViewController ()
@property(strong,nonatomic)UIScrollView *scrollView;
@property(strong,nonatomic)UIPageControl *pageControl;
//存储图片
@property(strong,nonatomic)UIImageView *firstIamges;
@property(strong,nonatomic)UIImageView *secondImage;
@property(strong,nonatomic)UIImageView *thirdImage;
//存储图片的集合
@property(strong,nonatomic)NSMutableArray *imageArray;
//当前页码
@property(assign,nonatomic)int currentPage;
@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/2.7, 300, WIDTH/5, 20)];
    
    //设置当前页
    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.firstIamges=[[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.firstIamges.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.firstIamges.image=[self.imageArray objectAtIndex:self.currentPage-1];
        self.secondImage.image=[self.imageArray objectAtIndex:self.currentPage];
        self.thirdImage.image=[self.imageArray objectAtIndex:0];
    }
    //中间页
    else{
    
        self.firstIamges.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.firstIamges.frame=CGRectMake(0, 0, WIDTH, 300);
    self.secondImage.frame=CGRectMake(WIDTH, 0, WIDTH, 300);
    self.thirdImage.frame=CGRectMake(WIDTH*2, 0, WIDTH, 300);
    
    
    [self.scrollView addSubview:self.firstIamges];
    [self.scrollView addSubview:self.secondImage];
    [self.scrollView addSubview:self.thirdImage];
    
    self.scrollView.contentOffset=CGPointMake(WIDTH, 0);
}

#pragma mark -Scrollview Delegate
//在滚动结束状态换图
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x=self.scrollView.contentOffset.x;
    
    //向左
    if (x<0||x==0) {
        if (self.currentPage==0) {
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS— UIScrollView和 UIPageCont.. 下一篇1016-06-首页20-封装工具条---UIT..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目