一. 创建一个继承uiview的类在.h里写
#import <UIKit/UIKit.h>
@class BannerView;
//#import 导入头文件 是把所有文件都传进来 @class 是只传入名字
@protocol BannerViewDelegate <NSObject>
@optional//可选
-(void)selectImage:(BannerView *)bannerView currentImage:(NSInteger)currentImage;
@end
@interface BannerView : UIView
@property(nonatomic, weak) id <BannerViewDelegate> delegate;
//自定义init方法
- (id)initWithFrame:(CGRect)frame addImageArray:(NSMutableArray *)addImageArray;
@end
二.在.m里写
#import "BannerView.h"
@interface BannerView ()<UIScrollViewDelegate>
{
CGFloat _width;//self的宽 私有变了用下划线开头
CGFloat _height;//self的高
}
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation BannerView
//自定义init方法 addImageArray是图片数组参数
//我们现在要获取self的宽度 这个宽度就是bannerview的宽度
- (id)initWithFrame:(CGRect)frame addImageArray:(NSMutableArray *)addImageArray{
self = [super initWithFrame:frame];
if (self) {
//获取self宽度
_width = self.frame.size.width;
//获取self高度
_height = self.frame.size.height;
//改变图片数组 1 2 3 4 5 6
self.dataArr = [NSMutableArray arrayWithArray:addImageArray];
//在数组的最后一位添加上第一张图片 1 2 3 4 5 6 1
[self.dataArr addObject:addImageArray[0]];
//在第一个位置插入图片6 // 6 1 2 3 4 5 6 1
[self.dataArr insertObject:addImageArray.lastObject atIndex:0];
//把scrollview添加到self上
[self addSubview:self.mainScrollView];
//添加page
[self addSubview:self.page];
//添加定时器
[self addTimer];
}
return self;
}
//添加定时器
- (void)addTimer{
self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//定时器方法
- (void)change{
//获取起始位置
CGFloat startX = self.mainScrollView.contentOffset.x;
//获取终点 // 3 1 2 3 1
CGFloat endX = startX + _width;
//在3的位置
if (endX == (self.dataArr.count - 1)* _width) {
//动画时长 animateWithDuration
[UIView animateWithDuration:0.25 animations:^{
//往最后一位走
self.mainScrollView.contentOffset = CGPointMake(endX, 0);
} completion:^(BOOL finished) {
//走向第二位
self.mainScrollView.contentOffset = CGPointMake(_width, 0);
//走到对应的点
NSInteger tmpPage = self.mainScrollView.contentOffset.x/_width;
//分页控制器当前的原点
self.page.currentPage = tmpPage - 1;
}];
}else{
[UIView animateWithDuration:0.25 animations:^{
self.mainScrollView.contentOffset = CGPointMake(endX, 0);
}];
//走到对应的点
NSInteger tmpPage = self.mainScrollView.contentOffset.x/_width;
//分页控制器当前的原点
self.page.currentPage = tmpPage - 1;
}
}
//初始化ScrollView
- (UIScrollView *)mainScrollView{
if (_mainScrollView == nil) {
_mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _width, _height)];
_mainScrollView.contentSize = CGSizeMake(_width*self.dataArr.count, _height);
//添加分页效果
_mainScrollView.pagingEnabled = YES;
//禁止弹簧效果
_mainScrollView.bounces = NO;
//禁止垂直滚动条
_mainScrollView.alwaysBounceVertical = YES;
for (int i = 0 ; i < self.dataArr.count; i ++) {
// 第一张x 0 第二张 _width 第三张 2*_width
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i *_width, 0, _width, _height)];
imageView.image = [UIImage imageNamed:self.dataArr[i]];
//给imageView添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtion:)];
[imageView addGestureRecognizer:tap];
//与用户交互
imageView.userInteractionEnabled = YES;
[_mainScrollView addSubview:imageView];
}
//起始页
_mainScrollView.contentOffset = CGPointMake(_width, 0);
//添加分页效果
_mainScrollView.pagingEnabled = YES;
//禁止弹簧效果
_mainScrollView.bounces = NO;
_mainScrollView.delegate = self;
_mainScrollView.showsHorizontalScrollIndicator = NO;
#warning bug
//禁止垂直滚动
_mainScrollView.showsHorizontalScrollIndicator = YES;
}
return _mainScrollView;
}
//点击imageView手势的方法
-(void)tapAtion:(UIGestureRecognizer *)recognizer{
//如果代理响应该方法,就走该方法
if ([self.delegate respondsToSelector:@selector(selectImage:currentImage:)]) {
[self.delegate selectImage:self currentImage:self.page.currentPage];
}
}
- (UIPageControl *)page{
if (_page == nil) {
_page = [[UIPageControl alloc] initWithFrame:CGRectMake(_width -100, _height - 30,100, 30)];
//小圆点的数量
_page.numberOfPages = self.dataArr.count - 2;
//不能点击
_page.userInteractionEnabled = NO;
_page.currentPageIndicatorTintColor = [UIColor blackColor];
}
return _page;
}
#pragma mark --- UIScrollViewDelegate
//将要拖拽的时候将timer停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (_timer) {
//timer停止在未来的一个时间
[_timer setFireDate:[NSDate distantFuture]];
}
}
//已经结束了
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (_timer) {
[_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}
//获取当前偏移量
CGPoint currentoffset = scrollView.contentOffset;
//如果是在最后一页的话让scrollView滚动到第一页
if (currentoffset.x == (self.dataArr.count -1)*_width) {
self.mainScrollView.contentOffset = CGPointMake(_width, 0);
}
//如果是第一页的时候,偏移量为0
if (currentoffset.x == 0) {
self.mainScrollView.contentOffset = CGPointMake(self.dataArr.count - 2*_width ,0 );
}
//获取新的偏移量
CGPoint newOffset = self.mainScrollView.contentOffset;
//获取偏移了多少宽度
NSInteger tmpPage = currentoffset.x / _width;
//设置当前pageControl的当前的点
self.page.currentPage = tmpPage - 1 ;
}
@end