iOS 使用UIView做轮播

转自:https://blog.csdn.net/wang6177/article/details/56289572

BannerView.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, readonly) UIScrollView *scrollView;

@property (nonatomic, readonly) UIPageControl *pageControl;

-(void)shouldAutoShow:(BOOL)shouldStart;
- (id)initWithFrame:(CGRect)frame;
-(void)setImageViewAry:(NSArray <UIView *>*)iViewAry;
-(void)setCurrentPage:(NSInteger)currentPage;
@end

@protocol BannerViewDelegate <NSObject>

@optional
- (void)didClickPage:(BannerView *)view atIndex:(NSInteger)index;//点击选中
-(void)selectPageIndex:(long)index;//当前显示的页码
@end

BannerView.m

//
//  BannerView.m
//  Library
//
//  Created by pactera on 16/12/12.
//  Copyright © 2016年 Pactera. All rights reserved.
//

#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 colorWithHexString:ThemeColor];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.backgroundColor = [UIColor clearColor];
        [self addSubview:_pageControl];
        [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(_viewWidth);
            make.height.mas_equalTo(20);
            make.left.equalTo(0);
            make.bottom.equalTo(self.bottom);
        }];
        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
{
    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];
        if(_viewList.count>(_currentPage+1)){
            _lastView = [_viewList objectAtIndex:_currentPage+1];
        }
    }
    else if (_currentPage == _viewList.count-1)
    {
        if((_currentPage-1)>=0){
            _firstView = [_viewList objectAtIndex:_currentPage-1];
        }
        _middleView = [_viewList objectAtIndex:_currentPage];
        _lastView = [_viewList firstObject];
    }
    else
    {
        if((_currentPage-1)>=0){
            _firstView = [_viewList objectAtIndex:_currentPage-1];
        }
        _middleView = [_viewList objectAtIndex:_currentPage];
        if(_viewList.count>(_currentPage+1)){
            _lastView = [_viewList objectAtIndex:_currentPage+1];
        }
    }
    
    //设置三个view的frame,加到scrollview上
    _firstView.frame = CGRectMake(0, 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);
    [_bannerdelegate selectPageIndex:_currentPage];//当前显示的页面序号
}

#pragma mark scrollvie停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //手动滑动时候暂停自动替换
    [_autoScrollTimer invalidate];
    _autoScrollTimer = nil;
    _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:10 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];
        if(_viewList.count>(_currentPage+1)){
            _lastView = [_viewList objectAtIndex:_currentPage+1];
        }
    }
    else if (_currentPage == _viewList.count-1)
    {
        if((_currentPage-1)>=0){
            _firstView = [_viewList objectAtIndex:_currentPage-1];
        }
        _middleView = [_viewList objectAtIndex:_currentPage];
        _lastView = [_viewList firstObject];
    }
    else
    {
        if((_currentPage-1)>=0){
            _firstView = [_viewList objectAtIndex:_currentPage-1];
        }
        _middleView = [_viewList objectAtIndex:_currentPage];
        if(_viewList.count>(_currentPage+1)){
            _lastView = [_viewList objectAtIndex:_currentPage+1];
        }
    }
    
    //设置三个view的frame,加到scrollview上
    _firstView.frame = CGRectMake(0, 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:10 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];
}
#pragma mark 设置当前页面
-(void)setCurrentPage:(NSInteger)currentPage{
    if(currentPage>-1&¤tPage<_viewList.count){
        _currentPage=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

使用方式

 _oneView = [[oneview alloc]init];
    _threeView = [[threeview alloc]init];
//    _twoView = [[twoview alloc]init];
//    _fourview = [[fourview alloc]init];
    
    BannerView *bannerview = [[BannerView alloc]initWithFrame:
                              CGRectMake(0, 74 + 345, self.view.frame.size.width, 301)];
    [bannerview setImageViewAry:@[_oneView,_threeView]];
    [bannerview shouldAutoShow:YES];
    [bannerview setCurrentPage:1];
    bannerview.bannerdelegate = self;
    [self.view addSubview:bannerview];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本Demo使用UICollectionView实现自动无限轮功能。 主要功能: 1.实现自动轮,可修改轮的时间 2.轮图片可以来自本地,也可来自网络,通过单独的方法进行设置即可。对于加载网络图片时,Demo中使用了YYWebImage,也可自行替换成SDWebImage。 3.重写了和系统UIPageControl一样的功能,可用图片代替PageControl上的点点,也可自定义其颜色以及切换动画。 使用方法:使用方法比较简单。 /** * 加载本地图片Banner */ - (void)setupLocalBannerImageView { NSArray *array = @[@"1.png", @"2.png", @"3.png", @"4.png", @"5.png"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) locationImageArray:array]; bannerVew.timeInterval = 2.0; [self.view addSubview:bannerVew]; } /** * 加载网络图片Banner */ - (void)setupNetWorkBannerImageView { NSArray *array = @[@"http://i3.download.fd.pchome.net/t_960x600/g1/M00/07/09/oYYBAFMv8q2IQHunACi90oB0OHIAABbUQAAXO4AKL3q706.jpg", @"http://images.weiphone.net/attachments/photo/Day_120308/118871_91f6133116504086ed1b82e0eb951.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/110425215921926a173e0f728e.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/1104241737046031b3a754f783.jpg"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 200) netWorkImageArray:array placeHolderImage:nil]; bannerVew.timeInterval = 2.0; bannerVew.pageControlStyle = FFPageControlStyleMiddle; bannerVew.delegate = self; [self.view addSubview:bannerVew]; } 以上方式即可简单使用,如需自定义PageControl也可继承FFAbstractDotView,些基本的设置即可。 gitHub下载地址:喜欢的朋友请给个星呗! 欢迎各位一起来讨论,有问题请发邮箱270452746@qq.com或者直接加我QQ:270452746进行讨论。谢谢!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值