iOS开发banner封装,圆点自定义为任意图片

释放双眼,带上耳机,听听看~!

1.效果图:

2.ZLImageViewDisplayView.h

//
//  ZLImageViewDisplayView.h
//  ZLImageViewDisplay
//
//  Created by weihong xuan on 15/8/14.
//  Copyright (c) 2015年 weihong xuan All rights reserved.
//
#import <UIKit/UIKit.h>
//点击图片的Block回调,参数当前图片的索引,也就是当前页数
typedef void(^TapImageViewButtonBlock)(NSInteger imageIndex);
@interface ZLImageViewDisplayView : UIView
{
    UIImage*currentImage;
    UIImage*otherImage;
}
//当前图片对应的圆点
@property (nonatomic, copy) NSString * currentImageStr;
//图片对应的圆点
@property (nonatomic, copy) NSString * otherImageStr;
//切换图片的时间间隔,可选,默认为3s
@property (nonatomic, assign) CGFloat scrollInterval;
//切换图片时,运动时间间隔,可选,默认为0.7s
@property (nonatomic, assign) CGFloat animationInterVale;
/**********************************
 *功能:便利构造器
 *参数:滚动视图的Frame, 要显示图片的数组
 *返回值:该类的对象
 **********************************/
+ (instancetype) zlImageViewDisplayViewWithFrame: (CGRect) frame
                                      WithImages: (NSArray *) images
                                      WithcurrentImageStr: (NSString *) currentImageStr
                                      WithotherImageStr: (NSString *) otherImageStr;
/**********************************
 *功能:便利初始化函数
 *参数:滚动视图的Frame, 要显示图片的数组
 *返回值:该类的对象
 **********************************/
- (instancetype)initWithFrame: (CGRect)frame
                   WithImages: (NSArray *) images WithcurrentImageStr: (NSString *) currentImageStr
            WithotherImageStr: (NSString *) otherImageStr;
/**********************************
 *功能:为每个图片添加点击时间
 *参数:点击按钮要执行的Block
 *返回值:无
 **********************************/
- (void) addTapEventForImageWithBlock: (TapImageViewButtonBlock) block;
#pragma 暂停定时器
-(void)resumeTimer;
#pragma 停止定时器
-(void)invalidateTimer;
@end

3.ZLImageViewDisplayView.m


//
//  ZLImageViewDisplayView.m
//  ZLImageViewDisplay
//
//  Created by weihong xuan on 15/8/14.
//  Copyright (c) 2015年 weihong xuan All rights reserved.
//
#import "ZLImageViewDisplayView.h"
#import <UIImageView+WebCache.h>
@interface ZLImageViewDisplayView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIPageControl *mainPageControl;
@property (nonatomic, assign) CGFloat widthOfView;
@property (nonatomic, assign) CGFloat heightView;
@property (nonatomic, strong) NSArray *imageViewArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) UIViewContentMode imageViewcontentModel;
@property (nonatomic, strong) UIPageControl *imageViewPageControl;
@property (nonatomic, strong) TapImageViewButtonBlock block;
@end
@implementation ZLImageViewDisplayView
#pragma -- 遍历构造器
+ (instancetype) zlImageViewDisplayViewWithFrame: (CGRect) frame
                                      WithImages: (NSArray *) images
                             WithcurrentImageStr: (NSString *) currentImageStr
                               WithotherImageStr: (NSString *) otherImageStr{
    ZLImageViewDisplayView *instance = [[ZLImageViewDisplayView alloc] initWithFrame:frame WithImages:images WithcurrentImageStr:currentImageStr WithotherImageStr:otherImageStr];
    return instance;
}
#pragma -- mark 遍历初始化方法
- (instancetype)initWithFrame: (CGRect)frame
                   WithImages: (NSArray *) images WithcurrentImageStr: (NSString *) currentImageStr
            WithotherImageStr: (NSString *) otherImageStr{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        _currentImageStr = currentImageStr;
        _otherImageStr = otherImageStr;
        
        //获取滚动视图的宽度
        _widthOfView = frame.size.width;
        
        //获取滚动视图的高度
        _heightView = frame.size.height;
        
        _scrollInterval = 3;
        
        _animationInterVale = 0.7;
        
        //当前显示页面
        _currentPage = 1;
        
        _imageViewcontentModel = UIViewContentModeScaleToFill;
        
        if (!images || images.count == 0) {
            images = @[@""];
        }
        
        self.clipsToBounds = YES;
        
        //初始化滚动视图
        [self initMainScrollView];
        
        //添加ImageView
        [self addImageviewsForMainScrollWithImages:images];
        
        //添加timer
        [self addPageControl];
        [self addTimerLoop];
    }
    return self;
}
- (void) addTapEventForImageWithBlock: (TapImageViewButtonBlock) block{
    if (_block == nil) {
        if (block != nil) {
            _block = block;
            
            [self initImageViewButton];
            
        }
    }
}
#pragma -- mark 初始化按钮
- (void) initImageViewButton{
    for ( int i = 0; i < _imageViewArray.count + 1; i ++) {
        CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
        UIButton *tempButton = [[UIButton alloc] initWithFrame:currentFrame];
        [tempButton addTarget:self action:@selector(tapImageButton:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            tempButton.tag = _imageViewArray.count;
        } else {
            tempButton.tag = i;
        }
        [_mainScrollView addSubview:tempButton];
    }
}
- (void) tapImageButton: (UIButton *) sender{
    if (_block) {
        _block(sender.tag);
    }
}
#pragma -- mark 初始化ScrollView
- (void) initMainScrollView{
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _widthOfView, _heightView)];
    
    _mainScrollView.contentSize = CGSizeMake(_widthOfView, _heightView);
    
    _mainScrollView.pagingEnabled = YES;
    
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    
    _mainScrollView.showsVerticalScrollIndicator = NO;
    
    _mainScrollView.delegate = self;
    
    [self addSubview:_mainScrollView];
}
#pragma 添加PageControl
- (void) addPageControl{
    _imageViewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _heightView - 20, _widthOfView, 20)];
    
    _imageViewPageControl.numberOfPages = _imageViewArray.count;
    
    _imageViewPageControl.currentPage = _currentPage - 1;
    
    _imageViewPageControl.tintColor = [UIColor blackColor];
    
    [self addSubview:_imageViewPageControl];
    currentImage = [UIImage imageNamed:_currentImageStr];
    otherImage = [UIImage imageNamed:_otherImageStr];
    [_imageViewPageControl setValue:otherImage forKeyPath:@"_pageImage"];
    [_imageViewPageControl setValue:currentImage forKeyPath:@"_currentPageImage"];
}
#pragma -- mark 给ScrollView添加ImageView
- (void)addImageviewsForMainScrollWithImages: (NSArray *) images{
    //设置ContentSize
    _mainScrollView.contentSize = CGSizeMake(_widthOfView * (images.count+1), _heightView);
    
    _imageViewArray = images;
   
    for ( int i = 0; i < _imageViewArray.count + 1; i ++) {
        
        CGRect currentFrame = CGRectMake(_widthOfView * i, 0, _widthOfView, _heightView);
        
        UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:currentFrame];
        
//        tempImageView.contentMode = _imageViewcontentModel;
        tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        tempImageView.clipsToBounds = YES;
        
        NSString *imageName;
        
        if (i == 0) {
            imageName = [_imageViewArray lastObject];
        } else {
            imageName = _imageViewArray[i - 1];
        }
        [tempImageView sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:@"testFangImage"]];
        
        [_mainScrollView addSubview:tempImageView];
    }
    _mainScrollView.contentOffset = CGPointMake(_widthOfView, 0);
    
}
- (void) addTimerLoop{
    
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:_scrollInterval target:self selector:@selector(pageOffset) userInfo:nil repeats:YES];
    }
}
- (void)pageOffset{
    _currentPage ++;
    if (_currentPage == _imageViewArray.count + 1) {
        _currentPage = 1;
    }
    [UIView animateWithDuration:_animationInterVale animations:^{
        _mainScrollView.contentOffset = CGPointMake(_widthOfView * _currentPage, 0);
    } completion:^(BOOL finished) {
        if (_currentPage == _imageViewArray.count) {
            _mainScrollView.contentOffset = CGPointMake(0, 0);
        }
    }];
     _imageViewPageControl.currentPage = _currentPage - 1;
    
    for (int i = 0;i < _imageViewArray.count; i++) {
        UIImageView *imageVieW = [_imageViewPageControl.subviews objectAtIndex:i];
        imageVieW.frame = CGRectMake(imageVieW.frame.origin.x, imageVieW.frame.origin.y, otherImage.size.width, otherImage.size.height);
        imageVieW.image = otherImage;
    }
    UIImageView *imageVieW = [_imageViewPageControl.subviews objectAtIndex:_currentPage - 1];
    imageVieW.frame = CGRectMake(imageVieW.frame.origin.x, imageVieW.frame.origin.y,currentImage.size.width, currentImage.size.height);
    imageVieW.image =  currentImage;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger currentPage = scrollView.contentOffset.x / _widthOfView;
    
    if(currentPage == 0){
        _mainScrollView.contentOffset = CGPointMake(_widthOfView * _imageViewArray.count, 0);
        _imageViewPageControl.currentPage = _imageViewArray.count;
        _currentPage = _imageViewArray.count;
    }
    
    if (_currentPage + 1 == currentPage || currentPage == 1) {
        _currentPage = currentPage;
        
        if (_currentPage == _imageViewArray.count + 1) {
            _currentPage = 1;
        }
        
        if (_currentPage == _imageViewArray.count) {
            _mainScrollView.contentOffset = CGPointMake(0, 0);
        }
        _imageViewPageControl.currentPage = _currentPage - 1;
        [self resumeTimer];
        
        for (int i = 0;i < _imageViewArray.count; i++) {
            UIImageView *imageVieW = [_imageViewPageControl.subviews objectAtIndex:i];
            imageVieW.frame = CGRectMake(imageVieW.frame.origin.x, imageVieW.frame.origin.y, otherImage.size.width, otherImage.size.height);
            imageVieW.image = otherImage;
        }
        UIImageView *imageVieW = [_imageViewPageControl.subviews objectAtIndex:_currentPage - 1];
        imageVieW.frame = CGRectMake(imageVieW.frame.origin.x, imageVieW.frame.origin.y,currentImage.size.width, currentImage.size.height);
        imageVieW.image =  currentImage;
        return;
    }
}
#pragma 暂停定时器
-(void)resumeTimer{
    if (![_timer isValid]) {
        return ;
    }
    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:_scrollInterval-_animationInterVale]];
}
#pragma 停止定时器
-(void)invalidateTimer{
    if (![_timer isValid]) {
        return ;
    }
    [_timer invalidate];
    _timer = nil;
}
@end

4.使用


NSMutableArray * arrImageData = [[NSMutableArray alloc]initWithObjects:@"http://118.178.122.109/public/uploads/20170602/67/563d3285678f4fd02e66d231e93cf73261c351.jpg",@"http://118.178.122.109/public/uploads/20170623/10/d6210541a898dc9f6da766a08e2b9d758e9d9d.jpg",@"http://118.178.122.109/public/uploads/20170309/c832712e054e25c8e2afe1685fb0fd07.jpg", nil];
    ZLImageViewDisplayView * imageViewDisplay = [ZLImageViewDisplayView zlImageViewDisplayViewWithFrame:CGRectMake(0, 0,self.view.frame.size.width,200) WithImages:arrImageData WithcurrentImageStr:@"compose_keyboard_dot_selected" WithotherImageStr:@"compose_keyboard_dot_normal"];
    imageViewDisplay.scrollInterval = 2;
    imageViewDisplay.animationInterVale = 0.6;
    [self.view addSubview:imageViewDisplay];
    [imageViewDisplay addTapEventForImageWithBlock:^(NSInteger imageIndex) {
        NSLog(@"广告页");
        
        
        
        
    }];

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索