封装一个简单的轮播图

WheelView的.h文件

#import <UIKit/UIKit.h>

// 协议方法用来点击轮播图

@protocol WheelViewDelegate <NSObject>

@optional

// 协议方法传一个参数是第几张图片

- (void)clickWheelView:(NSInteger)picNumber;

@end

@interface WheelView : UIScrollView

@property (nonatomic, assign) id<WheelViewDelegate> delegateA;

// 轮播图的初始化方法,参数数组需要传一个里面都是字符串网址的数组

- (instancetype)initWithFrame:(CGRect)frame DataArray:(NSMutableArray *)dataArray;

@end




WheelView的.m文件

#import "WheelView.h"

#import "UIImageView+WebCache.h"

@interface WheelView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *imgBefor;

@property (nonatomic, retain) UIImageView *imgMid;

@property (nonatomic, retain) UIImageView *imgNext;

@property (nonatomic, assign) NSInteger picBefor;

@property (nonatomic, assign) NSInteger picMid;

@property (nonatomic, assign) NSInteger picNext;

@property (nonatomic, assign) NSInteger picCount;

@property (nonatomic, retain) NSMutableArray *arrayPic;

@end

@implementation WheelView

- (instancetype)initWithFrame:(CGRect)frame DataArray:(NSMutableArray *)dataArray{

    self = [super initWithFrame:frame];

    if (self) {

        _picCount = dataArray.count - 1;

        _picBefor = dataArray.count - 1;

        _arrayPic = dataArray;

        _picMid = 0;

        _picNext = 1;

        [self createWheelView];

    }

    return self;

}

- (void)createWheelView{

    self.contentSize = CGSizeMake(3 * self.frame.size.width, self.frame.size.height);

    self.contentOffset = CGPointMake(self.frame.size.width, 0);

    self.showsHorizontalScrollIndicator = NO;

    self.pagingEnabled = YES;

    self.delegate = self;

    [self createImageView];

    [_imgBefor sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picBefor]]];

    [_imgMid sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picMid]]];

    [_imgNext sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picNext]]];

}

- (void)createImageView{

    _imgBefor = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    [self addSubview:_imgBefor];

    _imgMid = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

    [self addSubview:_imgMid];

    _imgMid.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];

    [_imgMid addGestureRecognizer:tap];

    _imgNext = [[UIImageView alloc] initWithFrame:CGRectMake(2 * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

    [self addSubview:_imgNext];

}

- (void)click{

    [self.delegateA clickWheelView:_picMid];

}

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

    if (self.contentOffset.x == 2 * self.frame.size.width) {

        self.contentOffset = CGPointMake(self.frame.size.width, 0);

        _picBefor++;

        _picMid++;

        _picNext++;

        if (_picBefor > _picCount) {

            _picBefor = 0;

        }

        if (_picMid > _picCount) {

            _picMid = 0;

        }

        if (_picNext > _picCount) {

            _picNext = 0;

        }

        [_imgBefor sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picBefor]]];

        [_imgMid sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picMid]]];

        [_imgNext sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picNext]]];

    } else if (self.contentOffset.x == 0) {

        self.contentOffset = CGPointMake(self.frame.size.width, 0);

        _picBefor--;

        _picMid--;

        _picNext--;

        if (_picBefor < 0) {

            _picBefor = _arrayPic.count - 1;

        }

        if (_picMid < 0) {

            _picMid = _arrayPic.count - 1;

        }

        if (_picNext < 0) {

            _picNext = _arrayPic.count - 1;

        }

        [_imgBefor sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picBefor]]];

        [_imgMid sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picMid]]];

        [_imgNext sd_setImageWithURL:[NSURL URLWithString:_arrayPic[_picNext]]];

    }

}


@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值