代碼寫的有點亂,後面可以根據自己的去加東西或者改什麽的;
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.
}
Tags: interface property protocol frame import
文章來源: