iOS 使用UICollectionView实现图片轮播

来源:互联网 发布:java nanotime 毫秒 编辑:程序博客网 时间:2024/05/02 14:24

1.因为UICollectionView继承于UIScrollView,所以我们可以使用UICollectionView来实现图片的无限轮播,当图片数量增加UICollectionView可以帮助我们减少内存的消耗。

#import "ViewController.h"static NSString *cellIdef_ = @"cellIdef";@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>@property (nonatomic,strong) UICollectionView *collectionView;@property (nonatomic,strong) UIPageControl   *pageControl;@property (nonatomic,strong) NSTimer           *timer;- (void)initialUSerInterface;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    [self initialUSerInterface];    // Do any additional setup after loading the view, typically from a nib.}//懒加载pageControl- (UIPageControl *)pageControl{    if (_pageControl == nil) {        //分页控件,本质上和scorllView没有任何关系,是2个独立的控件        _pageControl = [[UIPageControl alloc]init];        _pageControl.numberOfPages = 5;        CGSize size = [_pageControl sizeForNumberOfPages:5];        _pageControl.bounds = CGRectMake(0, 0, size.width, size.width);        _pageControl.center = CGPointMake(self.view.center.x, 130);        _pageControl.pageIndicatorTintColor = [UIColor redColor];        [self.view addSubview:_pageControl];    }    return _pageControl;}- (void)initialUSerInterface{    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];    layout.itemSize = CGSizeMake(self.view.frame.size.width, 250);    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 250) collectionViewLayout:layout];    collectionView.backgroundColor = [UIColor whiteColor];    collectionView.delegate = self;    collectionView.dataSource = self;    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdef_];    collectionView.pagingEnabled = YES;    collectionView.showsHorizontalScrollIndicator = NO;    collectionView.showsVerticalScrollIndicator = NO;    collectionView.bounces = NO;    collectionView.contentSize = CGSizeMake(5 * collectionView.bounds.size.width, 0);    [self.view addSubview:collectionView];    self.collectionView = collectionView;    [self addNSTime];    self.pageControl.currentPage = 0;}//添加定时器- (void)addNSTime{    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];    //添加到runloop中    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];    [timer fire];    self.timer = timer;}//删除定时器- (void)removeNSTimer{    [self.timer invalidate];    self.timer = nil;}//自动滚动- (void)nextPage{   NSInteger currentNumber = self.pageControl.currentPage;    CGFloat x = ((currentNumber + 1)%5) * self.collectionView.bounds.size.width;    if (currentNumber <= 5) {        [self.collectionView setContentOffset:CGPointMake(x, 0) animated:YES];    }else{        [self.collectionView setContentOffset:CGPointMake(x, 0) animated:NO];    }        self.pageControl.currentPage = x;}#pragma mark --)<UICollectionViewDataSource,UICollectionViewDelegate>- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 5;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdef_ forIndexPath:indexPath];    cell.backgroundColor = [UIColor yellowColor];    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld",indexPath.row + 1]]];    image.frame = cell.bounds;    [cell addSubview:image];    return cell;}//当用户开始拖拽的时候就调用- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{    [self removeNSTimer];}//当用户停止拖拽的时候调用- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    [self addNSTime];}//设置页码- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    int page = (int)(scrollView.contentOffset.x/scrollView.frame.size.width + 0.5)%5;    self.pageControl.currentPage = page;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
0 0
原创粉丝点击