iOS UIScrollView自动轮播图片效果(只需一行代码就可集成到自己的项目中)
1. 简介
效果使用到的UI: UIScrollView + UIPageControl + UIImageView + UILabel
本示例将UIScrollView自动轮播效果封装成一个自定义的控件,集成时只需一行代码就可以集成到自己的效果,初始化时只需给出数据源即可
2.使用步骤:
1. 将 LoopScrollImageView.h、LoopScrollImageView.h 拖到 自己的项目中
2. 在使用的时候调用初始化方法: - (id) initWithFrame:(CGRect)frame images:(NSArray *)images 即可完成轮播效果
3. 示例:
- (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, 64, self.view.frame.size.width, 130) images:imageDataSource];
[self.view addSubview:loopScrollImageView];
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#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
#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;
_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 -
#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 -
#pragma mark - Methods
- (void) startTimer {
[_timer setFireDate:[NSDate distantPast]];
}
- (void) stopTimer {
[_timer setFireDate:[NSDate distantFuture]];
}
@end
实现效果如图: