iOS - 轮播图

思路

  1. 先创建一个UIScrollView
  2. 设定UIPageControl
  3. 设定时间间隔(NSTimer)
  4. 因为轮播图需要跟着tableView一起向上滚动,所以UIScrollView是放在一个cell里面的

具体代码

  • ScrollerTableViewCell.h中
@interface ScrollerTableViewCell : UITableViewCell <UIScrollViewDelegate>//添加协议
//定义三个属性
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *time;

@end
  • ScrollerTableViewCell.m中
#import "ScrollerTableViewCell.h"

@implementation ScrollerTableViewCell

-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    //初始化_scrollView
    _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;
    //设置scollerView的画布长宽
    _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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值