ios广告栏的封装

刚忙完一个项目的上线,最近在抽空整理一些常用的控件准备写一个简单的框架出来。我今天要讲的是广告栏封装方法,我相信大家了解过广告栏,尤其是做电商项目的朋友,这个很常用一般是放在首页的位置。废话就不说了,直接切入主题吧。

广告栏是由哪几部分组成的:

UIScrollview和UIPageControl这两个控件组成。
直接上代码:

@property (retain, nonatomic) UIScrollView * imgScrollView;//图片的滚轮
@property (strong, nonatomic) UIPageControl * pageControl;//图片的页码
@property (retain, nonatomic) NSTimer * time;//定时滚动图片
@property (assign, nonatomic) NSInteger curIndex;//当前位置

然后进行初始化(这部分代码很简单)

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        line.backgroundColor = [UIColor clearColor];
        [self addSubview:line]; //UIScrollView(包括其子类),第一个添加到控制器的视图上才会预留空白,添加这个视图,取消留白
        _imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _imgScrollView.delegate = self;
        _imgScrollView.pagingEnabled = YES;
        _imgScrollView.bounces = YES;
        _imgScrollView.showsHorizontalScrollIndicator = NO;
        _imgScrollView.showsVerticalScrollIndicator = NO;
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 25, frame.size.width, 25)];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
        [self addSubview:_imgScrollView];
        [self addSubview:_pageControl];
        _curIndex = 0;
    }
    return self;
}

加载View部分代码


-(void)reloadView {
    if (self.pics.count == 0) {
        return;
    }
    CGFloat imgWidth = _imgScrollView.frame.size.width * (self.pics.count + (self.pics.count == 1 ? 0 : 1));
    _imgScrollView.contentSize = CGSizeMake(imgWidth, 0);
    //添加图片
    for (int i = 0; i <= (self.pics.count == 1 ? self.pics.count - 1 : self.pics.count); i++) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        NSString *urlStr = @"";
        if (i == 0) {
            urlStr = self.pics[self.pics.count - 1];
        }else {
            urlStr = self.pics[i - 1];
        }
        [self loadImage:urlStr imageView:imageView];
        [self setVFrame:i imageView:imageView];
        [_imgScrollView addSubview:imageView];
    }
    //初始化scrollView的滚动位置
    _imgScrollView.contentOffset = CGPointMake((self.pics.count == 1 ? 0 : self.imgScrollView.frame.size.width), 0);
    //pageController的个数
    _pageControl.numberOfPages = self.pics.count;
    //添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImgView:)];
    [_imgScrollView addGestureRecognizer:tap];
    //添加计时器
    [self addTimer];
}

图片的点击事件,我是用block来处理的

#pragma mark 点击事件
- (void)clickImgView:(UIGestureRecognizer *)sender {
    self.imgeViewSelectBlock(_curIndex);
}
- (void)returnIndex:(scrollViewSelectBlock)block{
    self.imgeViewSelectBlock = block;
}

图片自动滚动是加了一个定时器

#pragma mark 添加计时器
- (void)addTimer{
    if (self.pics.count != 1) {
        self.time = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    }
}
#pragma mark 下一张图片的方法
- (void)nextImage{
    if (_curIndex == self.pics.count - 1) {
        self.pageControl.currentPage = 0;
        _curIndex = 0;
    }else {
        self.pageControl.currentPage++;
        _curIndex++;
    }
    CGFloat x = (_curIndex + 1) * self.imgScrollView.frame.size.width;
    [UIView animateWithDuration:0.5 animations:^{
        _imgScrollView.contentOffset = CGPointMake(x, 0);
    } completion:^(BOOL finished) {
        if (finished && _curIndex == self.pics.count - 1) {
            _imgScrollView.contentOffset = CGPointMake(0, 0);
        }
    }];
}

上面是主要部分的代码Demo(在AdpScrollView文件里)
记得Star
下面是效果图

ios广告栏的封装_第1张图片
1.gif

你可能感兴趣的:(ios广告栏的封装)