IOS第六天(3:scrollView 圖片輪播器)

來源:互聯網
上載者:User

標籤:

IOS第六天(3:scrollView 圖片輪播器) 

#import "HMViewController.h"#define kImageCount     5@interface HMViewController () <UIScrollViewDelegate>@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIPageControl *pageControl;@property (nonatomic, strong) NSTimer *timer;@end@implementation HMViewController- (UIScrollView *)scrollView{    if (_scrollView == nil) {        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)];        _scrollView.backgroundColor = [UIColor redColor];                [self.view addSubview:_scrollView];                // 取消彈簧效果        _scrollView.bounces = NO;                // 取消水平捲軸        _scrollView.showsHorizontalScrollIndicator = NO;        _scrollView.showsVerticalScrollIndicator = NO;                // 要分頁        _scrollView.pagingEnabled = YES;                // contentSize        _scrollView.contentSize = CGSizeMake(kImageCount * _scrollView.bounds.size.width, 0);                // 設定代理        _scrollView.delegate = self;    }    return _scrollView;}- (UIPageControl *)pageControl{    if (_pageControl == nil) {        // 分頁控制項,本質上和scrollView沒有任何關係,是兩個獨立的控制項        _pageControl = [[UIPageControl alloc] init];        // 總頁數        _pageControl.numberOfPages = kImageCount;        // 控制項尺寸        CGSize size = [_pageControl sizeForNumberOfPages:kImageCount];                _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);        _pageControl.center = CGPointMake(self.view.center.x, 130);                // 設定顏色        _pageControl.pageIndicatorTintColor = [UIColor redColor];        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];                [self.view addSubview:_pageControl];                // 添加監聽方法        /** 在OC中,絕大多數"控制項",都可以監聽UIControlEventValueChanged事件,button除外" */        [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];    }    return _pageControl;}// 分頁控制項的監聽方法- (void)pageChanged:(UIPageControl *)page{    NSLog(@"%d", page.currentPage);        // 根據頁數,調整滾動視圖中的圖片位置 contentOffset    CGFloat x = page.currentPage * self.scrollView.bounds.size.width;    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];}// 視圖載入完成調用,通常用來設定資料- (void)viewDidLoad{    [super viewDidLoad];    // 設定圖片    for (int i = 0; i < kImageCount; i++) {        NSString *imageName = [NSString stringWithFormat:@"img_%02d", i + 1];        UIImage *image = [UIImage imageNamed:imageName];                UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];        imageView.image = image;                [self.scrollView addSubview:imageView];    }        // 計算imageView的位置    [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {                // 調整x => origin => frame        CGRect frame = imageView.frame;        frame.origin.x = idx * frame.size.width;                imageView.frame = frame;    }];//    NSLog(@"%@", self.scrollView.subviews);        // 分頁初始頁數為0    self.pageControl.currentPage = 0;        // 啟動時鐘    [self startTimer];}- (void)startTimer{    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];    // 添加到運行迴圈    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];}- (void)updateTimer{    // 頁號發生變化    // (當前的頁數 + 1) % 總頁數    int page = (self.pageControl.currentPage + 1) % kImageCount;    self.pageControl.currentPage = page;        NSLog(@"%d", self.pageControl.currentPage);    // 調用監聽方法,讓滾動視圖滾動    [self pageChanged:self.pageControl];}#pragma mark - ScrollView的代理方法// 滾動視圖停下來,修改頁面控制項的小點(頁數)- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    // 停下來的當前頁數    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));        // 計算頁數    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;        self.pageControl.currentPage = page;}/** 修改時鐘所在的運行迴圈的模式後,抓不住圖片  解決方案:抓住圖片時,停止時鐘,送售後,開啟時鐘 */- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    NSLog(@"%s", __func__);    // 停止時鐘,停止之後就不能再使用,如果要啟用時鐘,需要重新執行個體化    [self.timer invalidate];}- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    NSLog(@"%s", __func__);    [self startTimer];}@end

 

IOS第六天(3: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.