IOS scrollView 图片浏览

//

//  ViewController.m

//  0426

//

//  Created by apple on 15/4/26.

//  Copyright (c) 2015年 gense. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()<UIScrollViewDelegate>

{

    UIPageControl * pageControl;

    

    //定义图片名称集合

    NSMutableArray * arr ;

    

    //主显示图片view

    UIImageView * mainImageView ;

    

    //

    UIImageView * otherImageView;

    

    //定义当前显示索引位置

    int currentImageIndex ;

    

    

    UIImage * leftImage ;

    

    UIImage * rightImage;

}

@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    

    //取得屏幕宽度与高度

    CGFloat width = self.view.frame.size.width;

    CGFloat height = self.view.frame.size.height;

    

    arr = [NSMutableArray arrayWithCapacity:8];

    

    //添加滚动图片

    for (int i=0 ;i < 8; i++) {

        [arr addObject:[NSString stringWithFormat:@"0%d.jpg",i+1]];

    }

    

    

    currentImageIndex = 0;

    [self setImage];

    

    //实例化不显示图片

    mainImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:arr[0]]];

    

    mainImageView.frame = CGRectMake(width, 0, width, height);

    [_scrollView addSubview:mainImageView];

    

    otherImageView = [[UIImageView alloc] init];

    

    [_scrollView addSubview:otherImageView];

    

    

    //设置scrollview 滚动区域

    

    _scrollView.contentSize = CGSizeMake(3 * width, 0);

    

    //显示中间图片

    _scrollView.contentOffset = CGPointMake(width, 0);

    

    //隐藏水平滚动格

    _scrollView.showsHorizontalScrollIndicator = NO;

    

    

   _scrollView.pagingEnabled  = YES;

    

    //注册scrollview 的代理对象

    _scrollView.delegate = self;

    

    //添加分布条

    pageControl = [[UIPageControl alloc]init];

    

    //设置分页条位置

    pageControl.center = CGPointMake(width/2, height-39);

    

    pageControl.bounds = CGRectMake(0, 0, width, 30);

    

    //设置分页条 分页项的color

    pageControl.pageIndicatorTintColor = [UIColor whiteColor];

    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];

    

    

    pageControl.numberOfPages = 8;

    

    pageControl.currentPage = 0;

    

    //添加change事件

    

    [pageControl addTarget:self action:@selector(pageControlPageChange:) forControlEvents:UIControlEventValueChanged];

    

    [_scrollView addSubview:pageControl];

    

}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int currentPage =  scrollView.contentOffset.x/scrollView.frame.size.width ;

    

    if(currentPage == 1)

    {

        return ;

    }

    

    //向左滑

    if(currentPage == 0)

    {

        if( currentImageIndex == 0)

        {

            currentImageIndex = 7;

        }

        else

        {

            currentImageIndex--;

        }

        

        mainImageView.image = leftImage;

        _scrollView.contentOffset = CGPointMake(mainImageView.frame.origin.x, 0);

        

        

    }

    else if(2 == currentPage) //向右滑

    {

        if(currentImageIndex == 7)

        {

            currentImageIndex = 0;

        }

        else

        {

            currentImageIndex ++;

        }

        

        mainImageView.image = rightImage;

        _scrollView.contentOffset = CGPointMake(mainImageView.frame.origin.x, 0);

    }

    

    pageControl.currentPage = currentImageIndex ;

    

    

    //NSLog(@"current page index %d",currentImageIndex);

    

    //重新设置左右图片

    

    [self  setImage];

    

    



}



- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    NSLog(@"end scrolling Animation");

}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //调整pageControl bounds size x位置

    pageControl.center = CGPointMake( scrollView.contentOffset.x + scrollView.frame.size.width/2, scrollView.frame.size.height-39);

    

    

    //得到主显示图片的位置

    CGFloat mainImageX = mainImageView.frame.origin.x;

    

    //得到滚动offsetX

    CGFloat offsetX = _scrollView.contentOffset.x;

    

    //向右滚动

    if(offsetX < mainImageX)

    {

        otherImageView.image = leftImage;

        otherImageView.frame = CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);

    }

    else //向左滚动

    {

        otherImageView.image = rightImage;

        otherImageView.frame = CGRectMake(mainImageX*2, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);

    }

    

}





- (void) pageControlPageChange:(UIPageControl *) page

{

    [UIView animateWithDuration:0.5 animations:^{

        _scrollView.contentOffset = CGPointMake(page.currentPage*_scrollView.frame.size.width, 0);

    }];

    

    

    

}





#pragma mark 取得下一张图片

- (void) setImage

{

    

    if(currentImageIndex == 0)

    {

        leftImage = [UIImage imageNamed:arr[7]];

        rightImage = [UIImage imageNamed:arr[1]];

    }

    else if(currentImageIndex == 7)

    {

        leftImage = [UIImage imageNamed:arr[6]];

        rightImage = [UIImage imageNamed:arr[0]];

        

    }

    else

    {

        leftImage = [UIImage imageNamed:arr[currentImageIndex-1]];

        rightImage = [UIImage imageNamed:arr[currentImageIndex+1]];

    }

}







@end

 

你可能感兴趣的:(scrollview)