自定义图片轮播控件




#import <UIKit/UIKit.h>

@interface WImageScrollView : UIScrollView


//image的url或者文件名数组

@property (nonatomic,strong) NSArray *imageNames;


@end

//------------------------------------------------------------------------------

#import "WImageScrollView.h"


@interface WImageScrollView()<UIScrollViewDelegate>


@property (nonatomic,assign) NSInteger curIndex;

@property (nonatomic,strong) NSArray *imageViews;


@property (nonatomic,strong) UIPageControl *pageControl;


@end


@implementation WImageScrollView


-(void)awakeFromNib{

    [self setUp];

}


-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        [self setUp];
   
}

    return self;

}


-(instancetype)init{

    if (self = [super init]) {

        [self setUp];

    }

    return self;

}


//初始化配置

-(void)setUp{

    self.pagingEnabled = YES;

    self.delegate = self;

    self.showsHorizontalScrollIndicator = NO;

    self.showsVerticalScrollIndicator = NO;

}


//重写ImageNames的set方法

-(void)setImageNames:(NSArray *)imageNames{

    if (_imageNames) {

        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    }

    _imageNames = imageNames;

    if (self.pageControl.superview) {

        [self.pageControl removeFromSuperview];

    }

    self.curIndex = self.imageNames.count * 1000 +1;

    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];

    for (int i =0; i<3; i++) {

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH*i, 0, DEVICE_WIDTH, self.frame.size.height)];

        [arr addObject:iv];

        [self addSubview:iv];

    }

    self.imageViews = arr;

    self.contentSize = CGSizeMake(DEVICE_WIDTH*self.imageNames.count, 0);

    [self displayImage];

    [self performSelector:@selector(autoScroll) withObject:nil afterDelay:3.];

}


//加入pageControl

-(void)didMoveToSuperview{

    [super didMoveToSuperview];

    [self.superview addSubview:self.pageControl];

    [self.superview bringSubviewToFront:self.pageControl];

}


//重新布局

-(void)displayImage{

    UIImageView *pre = self.imageViews[0];

    UIImageView *cur = self.imageViews[1];

    UIImageView *next = self.imageViews[2];

    NSInteger index = (self.curIndex-1) % self.imageNames.count;

    if (index==0) {

        [self imageForImageView:pre imageName:self.imageNames[self.imageNames.count-1]];

        [self imageForImageView:next imageName:self.imageNames[index + 1]];

    }else if(index==self.imageViews.count-1){

        [self imageForImageView:pre imageName:self.imageNames[index - 1]];

        [self imageForImageView:next imageName:self.imageNames[0]];

    }else{

        [self imageForImageView:pre imageName:self.imageNames[index - 1]];

        [self imageForImageView:next imageName:self.imageNames[index + 1]];

    }

   [self imageForImageView:cur imageName:self.imageNames[index]];

    self.contentOffset = CGPointMake(DEVICE_WIDTH, 0);

    self.pageControl.currentPage = index;

    [self.superview bringSubviewToFront:self.pageControl];

}


//根据name来判断是网络图片还是本地图片,加载到imageview上面

-(void)imageForImageView:(UIImageView *)imageview imageName:(NSString *)name{

    if ([name hasPrefix:@"http"]) {

       
    }else{

        imageview.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]];

    }

}


//自动滚动

-(void)autoScroll{

    [self setContentOffset:CGPointMake(DEVICE_WIDTH*2, 0) animated:YES];

    [self performSelector:@selector(autoScroll) withObject:nil afterDelay:3.];

}


//手指开始拖动视图 取消自动滚动

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoScroll) object:nil];

    NSLog(@"scrollViewWillBeginDragging");
}


//手指结束拖拽 开始自动滚动

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    [self performSelector:@selector(autoScroll) withObject:nil afterDelay:3.];

}


//滚动结束后视图重新渲染

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGPoint offset = scrollView.contentOffset;

    NSInteger index = offset.x / DEVICE_WIDTH;

    if (index < 1) {

        if ((offset.x + DEVICE_WIDTH)/DEVICE_WIDTH <=1) {

            self.curIndex --;

            [self displayImage];

        }

    }else if(index >1){

        self.curIndex ++;

        [self displayImage];

    }

   
}


//重写pageControl的getter 懒加载

-(UIPageControl *)pageControl{
 
   if (!_pageControl) {
 
       CGFloat width = 20*self.imageNames.count;

        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-width)/2, self.frame.origin.y + self.frame.size.height-40, width, 15)];

        _pageControl.enabled = NO;

        _pageControl.numberOfPages = self.imageNames.count;

        _pageControl.currentPage = 0;

        _pageControl.pageIndicatorTintColor = kRGB(190, 190, 190);

        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

    }

    return _pageControl;

}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值