iOS-创建UIScrollerView(封装UIScrollerView)

创建继承于UIView的类WJImageScrollView,代码实现如下:

WJImageScrollView.h

#import <UIKit/UIKit.h>

/**点击图片block,参数当前图片索引*/
typedef void(^TapImageViewButtonBlock) (NSInteger imageIndex);

@interface WJImageScrollView : UIView
/**切换图片的时间间隔,可选,默认3s*/
@property(nonatomic,assign)CGFloat scrollInterval;
/**页面销毁时应该停止定时器,不然无法释放view 类dealloc时调用
 [timer invalidate]; 
 timer = nil;
 这个方法能解决问题但不明智,有违封装思想,实际使用中优化 */
@property(nonatomic,strong)NSTimer * timer;

/**
 *  创建轮播器:创建时调用这个方法
 *
 *  @param frame  滚动视图的frame
 *  @param images 要显示的图片数组
 *
 *  @return 类的对象
 */
+(instancetype)wjImageScrollViewWithFrame:(CGRect)frame
                               WithImages:(NSArray *)images;

/**
 *
 *
 *  @param frame  滚动视图的frame
 *  @param images 要显示的图片数组
 *
 *  @return 类的对象
 */
-(instancetype)initWithFrame:(CGRect)frame
                  WithIamges:(NSArray *)images;

-(void)addTapEventForImageWithBlock:(TapImageViewButtonBlock)block;
@end

WJImageScrollView.m

#import "WJImageScrollView.h"

@interface WJImageScrollView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * mainScrollView;

@property(nonatomic,assign)CGFloat widthView;

@property(nonatomic,assign)CGFloat hightView;

@property(nonatomic,strong)NSArray * imagesNameArray;

@property(nonatomic,assign)NSInteger currentPage;

@property(nonatomic,assign)UIViewContentMode  imageViewcontentModel;

@property(nonatomic,strong)UIPageControl * imageViewPageControl;

@property(nonatomic,strong)TapImageViewButtonBlock  block;

@end
@implementation WJImageScrollView

+(instancetype)wjImageScrollViewWithFrame:(CGRect)frame
                               WithImages:(NSArray *)images{
    
    WJImageScrollView * instance = [[WJImageScrollView alloc]
                                    initWithFrame:frame
                                    WithIamges:images];
    return instance;

}

-(instancetype)initWithFrame:(CGRect)frame
                  WithIamges:(NSArray *)images{
    
    self = [super initWithFrame:frame];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    if (self) {
        
        /**获取滚动视图的宽度*/
        _widthView = frame.size.width;
        
        /**获取滚动视图的高度*/
        _hightView = frame.size.height;
        
        _scrollInterval = 3;
        
        /**当前显示页面*/
        _currentPage = 0;
        
        _imageViewPageControl = UIViewContentModeScaleToFill;
        
        self.clipsToBounds = YES;
        
        _imagesNameArray = images;
        
        /**初始化滚动视图*/
        [self addSubview:self.mainScrollView];
        
        /**创建ImageView*/
        [self createImageView];
        
        /**添加imageViewPageControl*/
        [self addSubview:self.imageViewPageControl];
        
        /**添加timer*/
        [self startTimer];
    }
    return self;
}

-(void)addTapEventForImageWithBlock:(TapImageViewButtonBlock)block{
    if (self.block == nil) {
        if (block != nil) {
            self.block = block;
            [self initImageViewButton];
            
        }
    }
}

-(void)initImageViewButton{
    for (int i = 0; i <= _imagesNameArray.count; i++) {
        
        UIButton * imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(_widthView * i, 0, _widthView, _hightView)];
        imageBtn.tag = 900 + i;
        [imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainScrollView addSubview:imageBtn];
    }
}

#pragma mark - 初始化滚动视图

-(UIScrollView *)mainScrollView{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _widthView, _hightView)];
        _mainScrollView.contentSize = CGSizeMake(_widthView * _imagesNameArray.count, _hightView);
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.delegate = self;
        _mainScrollView.bounces = NO;
    }
    return _mainScrollView;
}

#pragma mark - 创建UIImageView
-(void)createImageView{
    
    for (int i = 0; i <= _imagesNameArray.count; i++) {
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_widthView * i, 0, _widthView, _hightView)];
        
        if (i == _imagesNameArray.count) {
            
            [self addImageToImageViewWithImageView:imageView WithIndex:0];
            
        }else{
            
            [self addImageToImageViewWithImageView:imageView WithIndex:i];
        }

        [self.mainScrollView addSubview:imageView];

    }
}

#pragma mark - 加载网络图片或者本地图片
-(void)addImageToImageViewWithImageView:(UIImageView *)imageView WithIndex:(NSInteger)index{
    [imageView setImage:[UIImage imageNamed:_imagesNameArray[index]]];
}

-(void)imageBtnClick:(UIButton *)btn{
    if (self.block) {
        self.block((btn.tag - 900) == self.imagesNameArray.count? 0:(btn.tag - 900));
    }
}

#pragma mark - 添加PageControl
-(UIPageControl *)imageViewPageControl{
    if (!_imageViewPageControl) {
        _imageViewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0,_hightView - 20, _widthView, 20)];
        _imageViewPageControl.numberOfPages = _imagesNameArray.count;
        _imageViewPageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _imageViewPageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _imageViewPageControl.currentPage = _currentPage;
    }
    return _imageViewPageControl;
}


-(void)changeOffset{
    
    if (self.mainScrollView.contentOffset.x / _widthView ==  _imagesNameArray.count ) {
        
        self.imageViewPageControl.currentPage = 0;
        [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else{
        
        [self.mainScrollView setContentOffset:CGPointMake(self.mainScrollView.contentOffset.x + _widthView, 0) animated:YES];
        
    }
}
#pragma mark - UIScollerView 代理

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger currentPageNum = (self.mainScrollView.contentOffset.x / _widthView) ;
    if (self.mainScrollView.contentOffset.x / _widthView == _imagesNameArray.count ) {
        
        self.imageViewPageControl.currentPage = 0;
        [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else{
        
        self.imageViewPageControl.currentPage = currentPageNum;
        
    }
    
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    
    [self colseTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{
    
    [self startTimer];
}
#pragma mark --- 开始计时
-(void)startTimer{
    
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:_scrollInterval target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

#pragma mark --- 停止计时
-(void)colseTimer
{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)dealloc{
    NSLog(@"WJImageScrollView释放");
}
@end

 使用方法

-(void) addWJImageScrollView{
    
    //获取要显示的位置
    CGRect screenFrame = [[UIScreen mainScreen] bounds];
    
    CGRect frame = CGRectMake(10, 160, screenFrame.size.width - 20, 200);
    
    NSArray *imageArray = @[@"001.jpg", @"002.jpg", @"003.jpg", @"004.jpg", @"005.jpg"];
    
    //初始化控件
    self.imageViewDisplay = [WJImageScrollView wjImageScrollViewWithFrame:frame WithImages:imageArray];
    
    //设定轮播时间
    self.imageViewDisplay.scrollInterval = 2;
    
    //把该视图添加到相应的父视图上
    [self.view addSubview:self.imageViewDisplay];
    
    [self.imageViewDisplay addTapEventForImageWithBlock:^(NSInteger imageIndex) {
        NSLog(@"点击了------%zd",imageIndex);
    }];
    
}

能解决问题的不好的方法释放view

-(void)dealloc{
    NSLog(@"释放");
    [self.imageViewDisplay.timer invalidate];
    self.imageViewDisplay.timer = nil;
}

 

posted @ 2016-08-19 11:31  J-Vijay  阅读(1062)  评论(0编辑  收藏  举报