本人~菜鸟一枚,这两天闲着无聊,就寻思自己写一下ScrollView自动无限循环,借鉴了不少别人写的,但发现还都不是很全面,自己整合一下,以备初学者们参观研习,废话不多说,直接上代。
首先是.h
@protocol ImageScrollViewDelegate <NSObject>
//@required //必须不必须看你自己了。
-(void)didSelectImageAtIndex:(NSInteger)index;//代理事件,需要把点击是哪个图片传出来。
@end
@interface ImageScrollView : UIView
@property(nonatomic, assign) id<ImageScrollViewDelegate> delegate;
-(ImageScrollView *)initWithFrame:(CGRect)frame ImageArray:(NSArray *)imgArr;//这个是初始化方法应该都可以看懂的!
@end
接下来是.m
#define screen_width [UIScreen mainScreen].bounds.size.width //自己定义的宏 ,屏幕宽度。
#define scren_Height [UIScreen mainScreen].bounds.size.height //屏幕高度。
@interface ImageScrollView ()<UIScrollViewDelegate>
{ //声明了几个需要的控件,这个不需要暴露在外面,所以写在.m里,别问我为什么不用属性,我就是看着好看,用着舒服。
CGRect scrollFrame;
NSTimer *_timer;//定时器,自动播放时候要用到。
NSInteger totalPage; //总共的页数。
NSInteger curPage;// 当前的page。
UIScrollView *mainScrollView;
UIPageControl *pageControl;
}
@property(nonatomic,strong) NSMutableArray *curImageArray;
@property(nonatomic, strong) NSArray *imgArray;
@end
@implementation ImageScrollView
-(ImageScrollView *)initWithFrame:(CGRect)frame ImageArray:(NSArray *)imgArr{
self = [super initWithFrame:frame];
if (self) {
//创建scrollview
scrollFrame = frame;
totalPage = imgArr.count;
curPage = 2;
self.curImageArray = [[NSMutableArray alloc</