代码写的有点乱,后面可以根据自己的去加东西或者改什么的;github地址:https://github.com/wang6177ming123/banner
1,使用UIView做轮播同样也是使用数组的形式加载view
2,代码如下.h文件
#import <UIKit/UIKit.h>
@protocol BannerViewDelegate;
@interface BannerView : UIView <UIScrollViewDelegate>
{
__unsafe_unretained id <BannerViewDelegate> _delegate;
}
@property (nonatomic, assign) id <BannerViewDelegate> bannerdelegate;
@property (nonatomic, assign) NSInteger currentPage;
//@property (nonatomic, strong) NSMutableArray *imageViewAry;
@property (nonatomic, readonly) UIScrollView *scrollView;
@property (nonatomic, readonly) UIPageControl *pageControl;
-(void)shouldAutoShow:(BOOL)shouldStart;
//-(instancetype)initWithViews:(NSArray <UIView *>*)views;
- (id)initWithFrame:(CGRect)frame;
-(void)setImageViewAry:(NSArray <UIView *>*)iViewAry;
//-(void)shouldAutoShow:(BOOL)shouldStart;
@end
@protocol BannerViewDelegate <NSObject>
@optional
- (void)didClickPage:(BannerView *)view atIndex:(NSInteger)index;
@end
3,.m文件
#import "BannerView.h"
@interface BannerView ()
{
UIView *_firstView;
UIView *_middleView;
UIView *_lastView;
float _viewWidth;
float _viewHeight;
NSTimer *_autoScrollTimer;
UITapGestureRecognizer *_tap;
BOOL startanimation;
}
@property (nonatomic, strong) NSArray <UIView *>* viewList;
@property (nonatomic) NSInteger showIndex;
@end
@implementation BannerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_viewWidth = self.bounds.size.width;
_viewHeight = self.bounds.size.height;
//设置scrollview
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _viewWidth, _viewHeight)];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(_viewWidth * 3, _viewHeight);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.delegate = self;
[self addSubview:_scrollView];
//设置分页
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, _viewWidth, 30)];
_pageControl.userInteractionEnabled = NO;
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
_pageControl.pageIndicatorTintColor = [UIColor whiteColor];
_pageControl.backgroundColor = [UIColor yellowColor];
[self addSubview:_pageControl];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tap];
}
return self;
}
#pragma mark 单击手势
-(void)handleTap:(UITapGestureRecognizer*)sender
{
if ([_bannerdelegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
[_bannerdelegate didClickPage:self atIndex:_currentPage+1];
}
}
#pragma mark 设置imageViewAry
-(void)setImageViewAry:(NSArray <UIView *>*)iViewAry
{
NSLog(@"imview===%@",iViewAry);
if (iViewAry) {
_viewList = iViewAry;
_currentPage = 0; //默认为第0页
_pageControl.numberOfPages = _viewList.count;
}
[self reloadData];
}
#pragma mark 刷新view页面
-(void)reloadData
{
if (!_firstView) {
NSLog(@"1");
startanimation = YES;
}else{
[UIView animateWithDuration:0.5 animations:^{
_firstView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
_middleView.frame = CGRectMake(0, 0, _viewHeight, _viewHeight);
_lastView.frame = CGRectMake(_viewWidth*2, 0, _viewWidth, _viewHeight);
NSLog(@"2");
startanimation = NO;
} completion:^(BOOL finished) {
}];
}
[_firstView removeFromSuperview];
[_middleView removeFromSuperview];
[_lastView removeFromSuperview];
//从数组中取到对应的图片view加到已定义的三个view中
if (_currentPage==0) {
_firstView = [_viewList lastObject];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList objectAtIndex:_currentPage+1];
}
else if (_currentPage == _viewList.count-1)
{
_firstView = [_viewList objectAtIndex:_currentPage-1];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList firstObject];
}
else
{
_firstView = [_viewList objectAtIndex:_currentPage-1];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList objectAtIndex:_currentPage+1];
}
//设置三个view的frame,加到scrollview上
_firstView.frame = CGRectMake(0, 0, _viewWidth, _viewHeight);
// _middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
_lastView.frame = CGRectMake(_viewWidth*2, 0, _viewWidth, _viewHeight);
[_scrollView addSubview:_firstView];
[_scrollView addSubview:_middleView];
[_scrollView addSubview:_lastView];
if (startanimation == YES) {
_middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
}else{
[UIView animateWithDuration:0.5 animations:^{
_middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
}];
}
//设置当前的分页
_pageControl.currentPage = _currentPage;
//显示中间页
_scrollView.contentOffset = CGPointMake(_viewWidth, 0);
}
#pragma mark scrollvie停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//手动滑动时候暂停自动替换
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
_autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_autoScrollTimer forMode:NSDefaultRunLoopMode];
//得到当前页数
float x = _scrollView.contentOffset.x;
//往前翻
if (x<=0) {
if (_currentPage-1<0) {
_currentPage = _viewList.count-1;
}else{
_currentPage --;
}
}
//往后翻
if (x>=_viewWidth*2) {
if (_currentPage==_viewList.count-1) {
_currentPage = 0;
}else{
_currentPage ++;
}
}
[self reload];
}
-(void)reload
{
startanimation = YES;
[_firstView removeFromSuperview];
[_middleView removeFromSuperview];
[_lastView removeFromSuperview];
//从数组中取到对应的图片view加到已定义的三个view中
if (_currentPage==0) {
_firstView = [_viewList lastObject];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList objectAtIndex:_currentPage+1];
}
else if (_currentPage == _viewList.count-1)
{
_firstView = [_viewList objectAtIndex:_currentPage-1];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList firstObject];
}
else
{
_firstView = [_viewList objectAtIndex:_currentPage-1];
_middleView = [_viewList objectAtIndex:_currentPage];
_lastView = [_viewList objectAtIndex:_currentPage+1];
}
//设置三个view的frame,加到scrollview上
_firstView.frame = CGRectMake(0, 0, _viewWidth, _viewHeight);
// _middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
_lastView.frame = CGRectMake(_viewWidth*2, 0, _viewWidth, _viewHeight);
if (startanimation == YES) {
_middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
}else{
[UIView animateWithDuration:0.5 animations:^{
_middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
}];
}
[_scrollView addSubview:_firstView];
[_scrollView addSubview:_middleView];
[_scrollView addSubview:_lastView];
//设置当前的分页
_pageControl.currentPage = _currentPage;
//显示中间页
_scrollView.contentOffset = CGPointMake(_viewWidth, 0);
}
#pragma mark 自动滚动
-(void)shouldAutoShow:(BOOL)shouldStart
{
if (shouldStart) //开启自动翻页
{
if (!_autoScrollTimer) {
_autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_autoScrollTimer forMode:NSDefaultRunLoopMode];
}
}
else //关闭自动翻页
{
if (_autoScrollTimer.isValid) {
[_autoScrollTimer invalidate];
_autoScrollTimer = nil;
}
}
}
#pragma mark 展示下一页
-(void)autoShowNextImage
{
if (_currentPage == _viewList.count-1) {
_currentPage = 0;
}else{
_currentPage ++;
}
[self reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
4.最后是使用
#import "BovViewController.h"
#import "BannerView.h"
#import "oneview.h"
#import "twoview.h"
#import "threeview.h"
#import "fourview.h"
@interface BovViewController ()<UIScrollViewDelegate,BannerViewDelegate>
@property (strong , nonatomic) oneview *oneView;
@property (strong , nonatomic) twoview *twoView;
@property (strong , nonatomic) threeview *threeView;
@property (strong , nonatomic) fourview *fourview;
@property (nonatomic, strong) NSArray <UIView *>* viewList;
@end
@implementation BovViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = kDefaultBackgroundColor;
// Do any additional setup after loading the view.
//设置视图的背景颜色
// self.view.backgroundColor = [UIColor blackColor];
//调用 setuoPage方法
_oneView = [[oneview alloc]init];
_threeView = [[threeview alloc]init];
_twoView = [[twoview alloc]init];
_fourview = [[fourview alloc]init];
// _viewList = @[_oneView,_twoView,_threeView];
// [self setupPage:nil];
BannerView *bannerview = [[BannerView alloc]initWithFrame:
CGRectMake(0, Begin_Top_Y + 345, W, 301)];
[bannerview setImageViewAry:@[_oneView,_twoView,_threeView,_fourview]];
[bannerview shouldAutoShow:YES];
bannerview.bannerdelegate = self;
// bannerview.frame = CGRectMake(0, Begin_Top_Y + 345, W, 301);
bannerview.backgroundColor = [UIColor redColor];
[self.view addSubview:bannerview];
}
//改变滚动视图的方法实现
- (void)didClickPage:(BannerView *)view atIndex:(NSInteger)index{
NSLog(@"banner==%ld",(long)index);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}