iOS 有關自動輪播圖片

來源:互聯網
上載者:User

標籤:

  //初始化當前視圖        _currentImageView = [[UIImageView alloc] init];        [_currentImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:0]]];        _currentImageView.frame = CGRectMake(myScroller.frame.size.width, 0, myScroller.frame.size.width,180);//        _currentImageView.contentMode = UIViewContentModeScaleAspectFill;        [myScroller addSubview:_currentImageView];                _nextImageView = [[UIImageView alloc] init];        [_nextImageView  setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:1]]];        _nextImageView.frame = CGRectMake(myScroller.frame.size.width * 2, 0,  myScroller.frame.size.width,180);//        _nextImageView.contentMode = UIViewContentModeScaleAspectFill;        [myScroller addSubview:_nextImageView];                //初始化上一個視圖        _previousView = [[UIImageView alloc] init];        [_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:3]]];        _previousView.frame = CGRectMake(0, 0,myScroller.frame.size.width,180);//        _previousView.contentMode = UIViewContentModeScaleAspectFill;               [myScroller addSubview:_previousView];        }       {        SMPageControl *pageControl = [[SMPageControl alloc] init];        [pageControl setFrame:CGRectMake(0, myScroller.frame.size.height+64 - 10.0f,myScroller.frame.size.width,10.0f)];        [pageControl setNumberOfPages:4];        [pageControl setBackgroundColor:[UIColor clearColor]];        [pageControl setCurrentPage:0];        [pageControl setCurrentPageIndicatorTintColor:[UIColor lightGrayColor]];        [pageControl setPageIndicatorTintColor:[UIColor grayColor]];        myPageControl=pageControl;        [self.view addSubview:myPageControl];    }if (![myTimer isValid]) {        myTimer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateAuto) userInfo:nil repeats:YES];    }#pragma mark 時鐘動畫調用方法- (void)updateAuto{    if (_isDraging) {        return;    }        CGPoint offset = myScroller.contentOffset;        offset.x += self.view.frame.size.width;    if (offset.x > self.view.frame.size.width*2) {        offset.x = self.view.frame.size.width;    }        [myScroller setContentOffset:offset animated:YES];}#pragma mark - 代理方法 #pragma mark 準備開始拖動 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {         _isDraging = YES;     } #pragma mark 視圖停止滾動 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {         _isDraging = NO;}#pragma mark 已經拖動- (void)scrollViewDidScroll:(UIScrollView *)scrollView{        static int i = 0;//當前展示的是第幾張圖片       float offset = scrollView.contentOffset.x;    if (_nextImageView.image == nil || _previousView.image == nil) {                //載入下一個視圖        [_nextImageView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == kCount ? 0: i+1]]];                //載入上一個視圖        [_previousView setImageWithURL:[NSURL URLWithString:[array_pic objectAtIndex:i == 0 ? kCount : i - 1]]];    }        if (offset == 0) {        _currentImageView.image = _previousView.image;        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);        _previousView.image = nil;                if (i == 0) {            i = kCount;        }else{            i -= 1;        }            }        if (offset == scrollView.bounds.size.width * 2) {        _currentImageView.image = _nextImageView.image;        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);        _nextImageView.image = nil;                if (i == kCount) {            i = 0;  //如果當前圖片索引在最後一個,則重設為0,否則+1        }else{            i += 1;        }    }     [myPageControl setCurrentPage:i];}

 

iOS 有關自動輪播圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.