IOS UIScrollView實現自動輪播圖功能

//
//  LoopScrollImageView.h
//  test07 實現自動輪播圖效果
//
//  Created by mouweng on 17/8/26.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LoopScrollImageView : UIView <UIScrollViewDelegate>
    @property (nonatomic, retain) NSTimer * timer;  //設置定時器
    
    @property (nonatomic, retain) UIScrollView * scrollView;
    @property (nonatomic, retain) UIPageControl * pageControl;
    @property (nonatomic, retain) UILabel * currentPageLabel;
    
    @property (nonatomic, retain) NSArray * dataSource;
    
    
    - (id) initWithFrame:(CGRect)frame images:(NSArray *)images;
    
    - (void) startTimer;
    - (void) stopTimer;


@end


//
//  LoopScrollImageView.m
//  test07 實現自動輪播圖效果
//
//  Created by mouweng on 17/8/26.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "LoopScrollImageView.h"

@implementation LoopScrollImageView

//重寫初始化方法
- (id) initWithFrame:(CGRect)frame images:(NSArray *)images {
    if (self = [super initWithFrame:frame]) {
        self.dataSource = images;
        // 1. 初始化定時器
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(loopImageTimer:) userInfo:nil repeats:YES];
        //[timer setFireDate:[NSDate distantFuture]];
        
        CGFloat viewWidth = CGRectGetWidth(self.frame);
        CGFloat viewHeigh = CGRectGetHeight(self.frame);
        NSUInteger pageCount = images.count;
        
        // 2. 初始化UIScrollView
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeigh)];
        _scrollView.contentSize = CGSizeMake(viewWidth * pageCount, viewHeigh);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        
        for (int i = 0; i < images.count; i++) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(viewWidth * i, 0, viewWidth, viewHeigh)];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = [UIImage imageNamed:images[i]];
            imageView.tag = i;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loopImageOnClicked:)];
            [imageView addGestureRecognizer:tap];
            
            [_scrollView addSubview:imageView];
        }
        
        [self addSubview:_scrollView];
        
        if (images.count > 1) {
            // 3. 初始化UIPageControl
            int pageControllHeight = 30;
            CGFloat scrollViewBottom = _scrollView.frame.origin.y + _scrollView.frame.size.height;
            _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, scrollViewBottom - pageControllHeight, 100, pageControllHeight)];
            _pageControl.currentPage = 0;//當前頁數所在的page位置
            _pageControl.numberOfPages = pageCount;
            _pageControl.backgroundColor = [UIColor grayColor];
            [self addSubview:_pageControl];
            
            // 4. 初始化 當前頁/總頁數
            _currentPageLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth - 50, _pageControl.frame.origin.y, 50, pageControllHeight)];
            _currentPageLabel.text = [NSString stringWithFormat:@"%d/%ld", 1, images.count];
            [self addSubview:_currentPageLabel];
        }
    }
    
    return self;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isMemberOfClass:[UITableView class]]) {
        
    }else {
        int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width;   //當前是第幾個視圖
        _pageControl.currentPage = index;
        
        _currentPageLabel.text = [NSString stringWithFormat:@"%d/%ld", index + 1, self.dataSource.count];
    }
}


//循環定時器設置
#pragma mark - Actions
- (void) loopImageTimer:(NSTimer *)timer {
    NSUInteger totalNum = self.dataSource.count;
    if (totalNum > 1) {
        CGPoint newOffset = _scrollView.contentOffset;
        newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
        if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (totalNum-1))) {
            newOffset.x = 0 ;
        }
        int index = newOffset.x / CGRectGetWidth(_scrollView.frame);   //當前是第幾個視圖
        newOffset.x = index * CGRectGetWidth(_scrollView.frame);
        
        _pageControl.currentPage = index;
        _currentPageLabel.text = [NSString stringWithFormat:@"%d / %ld",index+1, totalNum];
        [_scrollView setContentOffset:newOffset animated:YES];
    }else{
        [_timer setFireDate:[NSDate distantFuture]];//關閉定時器
    }
}


//點擊事件
- (void) loopImageOnClicked:(UITapGestureRecognizer *) tapGestureRecognizer{
    NSInteger imageViewTag = tapGestureRecognizer.view.tag;
    NSLog(@"imageViewTag: ----------------- %ld", imageViewTag);        // 0, 1, 2
    
}


//開始和停止
#pragma mark - Methods
- (void) startTimer {
    [_timer setFireDate:[NSDate distantPast]];
}

- (void) stopTimer {
    [_timer setFireDate:[NSDate distantFuture]];
}

@end


//
//  ViewController.m
//  test07 實現自動輪播圖效果
//
//  Created by mouweng on 17/8/26.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "ViewController.h"
#import "LoopScrollImageView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"UIImage自動輪播";
    
    NSArray *  imageDataSource = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", nil];
    // 只需初始化就可以實現效果
    LoopScrollImageView * loopScrollImageView =[[LoopScrollImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 736) images:imageDataSource];
    [self.view addSubview:loopScrollImageView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章