.h
//// ImageScrollView.h// StarrySky//// Created by 于泽峰 on 2017/9/12.// Copyright © 2017年 BW. All rights reserved.//#import///
表示图片滚动视图的样式typedef enum { ImageScrollType_Guide = 100,
// 开机guide视图样式 ImageScrollType_Banner // banner栏样式}ImageScrollType;/// 协议,触发事件回调的方法@protocol ImageScrollViewDelegate@optional/// “ImageScrollType_Guide”样式的最后一个视图上的“立即体验”按钮触发-(void)experienceDidHandle;
/// “ImageScrollType_Banner”样式每一个图片触发回调的方法;index表示选中的图片的下标
-(void)bannerImageDidHandleWithIndex:(NSInteger)index;@end@interface ImageScrollView : UIScrollView
/// 参数1:滚动视图位置(建议以4.7寸屏设置); 参数2:样式 ; 参数3:图片名称数组 ; 参数4:guide样式的“立即体验”按钮标题;参数5:guide样式的“立即体验”按钮位置;参数6:banner轮播图时间间隔 ; 参数7:代理设置,用于触发事件回调-(instancetype)initWithFrame:(CGRect)frame style:(ImageScrollType)type images:(NSArray *)imgNameArr confirmBtnTitle:(NSString *)title confirmBtnTitleColor:(UIColor *)titleColor confirmBtnFrame:(CGRect)btnFrame autoScrollTimeInterval:(NSTimeInterval)interval delegate:(id)delegate;
/// 给ImageScrollView的父视图添加分页控件,这个必须在把ImageScrollView加为父视图的子视图之后再调用此方法
-(void)addPageControlToSuperView:(UIView *)superView;
@end
.m
//// ImageScrollView.m// StarrySky//// Created by 于泽峰 on 2017/9/12.// Copyright © 2017年 BW. All rights reserved.
//#import "ImageScrollView.h"
#define SCR_W [UIScreen mainScreen].bounds.size.width // 屏幕宽度#define SCR_H [UIScreen mainScreen].bounds.size.height // 屏幕高度#define FIT_X(x) (SCR_W/375.*(x)) // 用于x轴适配#define FIT_Y(y) (SCR_H/667.*(y)) // 用于y轴适配 @interface ImageScrollView (){ ImageScrollType _type; // 样式 NSString *_confirmBtnTitle; // 确认按钮标题 CGRect _confirmBtnFrame; // 确认按钮位置 UIColor *_confirmBtnTitleColor; // 确认按钮标题颜色 NSUInteger _imgCount; // 图片个数 CGFloat _disX; // 在X轴的移动距离}@property(nonatomic,assign)idscDelegate;@property(nonatomic,strong)NSTimer *myTimer; // 定时器@property(nonatomic,strong)UIPageControl *pageCtl; // 分页控件@end@implementation ImageScrollView#pragma mark - 对外接口/// 实例化方法-(instancetype)initWithFrame:(CGRect)frame style:(ImageScrollType)type images:(NSArray *)imgNameArr confirmBtnTitle:(NSString *)title confirmBtnTitleColor:(UIColor *)titleColor confirmBtnFrame:(CGRect)btnFrame autoScrollTimeInterval:(NSTimeInterval)interval delegate:(id)delegate {
self = [super initWithFrame:frame];
if (self) {
if(type == ImageScrollType_Guide){
// 设置内容视图大小
self.contentSize = CGSizeMake(frame.size.width * imgNameArr.count, frame.size.height);
}
else {
// 设置内容视图大小
self.contentSize = CGSizeMake(frame.size.width * (imgNameArr.count+2), frame.size.height);
}
self.directionalLockEnabled = YES;
// 样式赋值
_type = type;
// 图片数量赋值
_imgCount = imgNameArr.count;
// guide样式固定大小
if (type == ImageScrollType_Guide) {
self.frame = CGRectMake(0, 0, SCR_W, SCR_H);
self.contentSize = CGSizeMake(SCR_W * imgNameArr.count, SCR_H);
_confirmBtnTitle = title;
_confirmBtnFrame = btnFrame;
_confirmBtnTitleColor = titleColor;
}
// 隐藏滚动条
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
// 开启分页属性
self.pagingEnabled = YES;
// 设置UIScrollView代理
self.delegate = self;
// 设置自定义的代理
self.scDelegate = delegate;
// 添加图片子视图
[self addImageSubViews:imgNameArr];
// 添加定时器
if(type == ImageScrollType_Banner) {
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerDidHandle:) userInfo:nil repeats:YES];
}
}
return self;
}
/// 添加分页控件方法
-(void)addPageControlToSuperView:(UIView *)superView {
[superView addSubview:self.pageCtl];
}
#pragma mark - 分页控制器实例化getter重写
/// 添加分页子视图
-(UIPageControl *)pageCtl{
if (!_pageCtl) {
_pageCtl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, FIT_X(300), FIT_Y(25))];
if (_type == ImageScrollType_Guide) {
_pageCtl.center = CGPointMake(SCR_W/2, SCR_H-FIT_Y(30));
}
else{
_pageCtl.center = CGPointMake(self.frame.origin.x+self.frame.size.width/2, self.frame.origin.y+self.frame.size.height-FIT_Y(25));
}
_pageCtl.pageIndicatorTintColor = [UIColor grayColor];
_pageCtl.currentPageIndicatorTintColor = [UIColor redColor];
_pageCtl.hidesForSinglePage = YES;
_pageCtl.numberOfPages = _imgCount;
_pageCtl.currentPage = 0;
}
return _pageCtl;
}
#pragma mark - 添加图片子视图
/// 添加图片子视图
-(void)addImageSubViews:(NSArray *)imgNameArr
{
// guide样式
if (_type == ImageScrollType_Guide) {
for (int i = 0; i