思路
- 先创建一个UIScrollView
- 设定UIPageControl
- 设定时间间隔(NSTimer)
- 因为轮播图需要跟着tableView一起向上滚动,所以UIScrollView是放在一个cell里面的
具体代码
@interface ScrollerTableViewCell : UITableViewCell <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *time;
@end
#import "ScrollerTableViewCell.h"
@implementation ScrollerTableViewCell
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 414, 200)];
[self.contentView addSubview:_scrollView];
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(150, 180, 40, 10)];
[self.contentView addSubview:_pageControl];
for (int i = 0; i < 4; i++) {
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(414 * i, 0, 414, 200);
NSString *image = [NSString stringWithFormat:@"main_img%d.png", i+1];
imageView.image = [UIImage imageNamed:image];
[_scrollView addSubview:imageView];
}
_time = [[NSTimer alloc]init];
_time = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(repeat) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_time forMode:NSDefaultRunLoopMode];
return self;
}
-(void)layoutSubviews {
_pageControl.numberOfPages = 4;
_pageControl.currentPage = 0;
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
_pageControl.pageIndicatorTintColor = [UIColor blackColor];
_scrollView.bounces = NO;
_scrollView.userInteractionEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.contentSize = CGSizeMake(414 * 4, 200);
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
}
-(void) repeat{
NSInteger num = self.pageControl.currentPage;
if (num == 3) {
num = 0;
_scrollView.contentOffset = CGPointMake(0, 0);
[_scrollView setContentOffset:_scrollView.contentOffset animated:YES];
}else{
num++;
}
CGFloat offset = num * [UIScreen mainScreen].bounds.size.width;
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentOffset = CGPointMake(offset, 0);
}];
}
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.time invalidate];
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
_time = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(repeat) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_time forMode:NSDefaultRunLoopMode];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (_scrollView.contentOffset.x == 0) {
_pageControl.currentPage = 0;
}
if (_scrollView.contentOffset.x == 414) {
_pageControl.currentPage = 1;
}
if (_scrollView.contentOffset.x == 414 * 2) {
_pageControl.currentPage = 2;
}
if (_scrollView.contentOffset.x == 414 * 3) {
_pageControl.currentPage = 3;
}
}
@end