IOS scrollView 圖片瀏覽

來源:互聯網
上載者:User

標籤:

////  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

 

IOS scrollView 圖片瀏覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.