一个完整的scrollView循环展示封装

本文介绍了一个用于iOS应用中的广告轮播视图组件,该组件基于UITableView的HeaderView实现,支持自动翻页和用户交互。文章提供了完整的代码实现,便于开发者快速集成。

可用于tableview的headerview,展示广告或者动态图片之类的.话不多说,直接上代码

#import <UIKit/UIKit.h>
@protocol WHcrollViewViewDelegate;
@interface WHScrollAndPageView : UIView<UIScrollViewDelegate>
{
    id <WHcrollViewViewDelegate> __delegate;
}
@property (nonatomic, assign) id <WHcrollViewViewDelegate> delegate;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray *imageViewAry;
@property (nonatomic, readonly) UIScrollView *scrollView;
@property (nonatomic, readonly) UIPageControl *pageControl;

-(void)shouldAutoShow:(BOOL)shouldStart;
@end

@protocol WHcrollViewViewDelegate <NSObject>
@optional
- (void)didClickPage:(WHScrollAndPageView *)view atIndex:(NSInteger)index;
@end

#import "WHScrollAndPageView.h"

@interface WHScrollAndPageView ()

{

    UIView *_firstView;

    UIView *_middleView;

    UIView *_lastView;

    float _viewWidth;

    float _viewHeight;

    NSTimer *_autoScrollTimer;

    UITapGestureRecognizer *_tap;

}

@end

@implementation WHScrollAndPageView

- (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 blackColor];

        _scrollView.delegate = self;

        [self addSubview:_scrollView];

        //设置分页

        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _viewHeight-30, _viewWidth, 30)];

        _pageControl.userInteractionEnabled = NO;

        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];

        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];

        [self addSubview:_pageControl];

        //设置单击手势

        _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

        _tap.numberOfTapsRequired = 1;

        _tap.numberOfTouchesRequired = 1;

        [_scrollView addGestureRecognizer:_tap];

    }

    return self;

}

#pragma mark 单击手势

-(void)handleTap:(UITapGestureRecognizer*)sender

{

    if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {

        [_delegate didClickPage:self atIndex:_currentPage+1];

    }

}

#pragma mark 设置imageViewAry

-(void)setImageViewAry:(NSMutableArray *)imageViewAry

{

    if (imageViewAry) {

        _imageViewAry = imageViewAry;

        _currentPage = 0; //默认为第0

        _pageControl.numberOfPages = _imageViewAry.count;

    }

    [self reloadData];

}

#pragma mark 刷新view页面

-(void)reloadData

{

    [_firstView removeFromSuperview];

    [_middleView removeFromSuperview];

    [_lastView removeFromSuperview];   

    //从数组中取到对应的图片view加到已定义的三个view

    if (_currentPage==0) {

        _firstView = [_imageViewAry lastObject];

        _middleView = [_imageViewAry objectAtIndex:_currentPage];

        _lastView = [_imageViewAry objectAtIndex:_currentPage+1];

    }

    else if (_currentPage == _imageViewAry.count-1)

    {

        _firstView = [_imageViewAry objectAtIndex:_currentPage-1];

        _middleView = [_imageViewAry objectAtIndex:_currentPage];

        _lastView = [_imageViewAry firstObject];

    }

    else   {

        _firstView = [_imageViewAry objectAtIndex:_currentPage-1];

        _middleView = [_imageViewAry objectAtIndex:_currentPage];

        _lastView = [_imageViewAry objectAtIndex:_currentPage+1];

    }    

    //设置三个viewframe,加到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];

    //设置当前的分页

    _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];   

    //得到当前页数

    float x = _scrollView.contentOffset.x; 

    //往前翻

    if (x<=0) {

        if (_currentPage-1<0) {

            _currentPage = _imageViewAry.count-1;

        }else{

            _currentPage --;

        }

    }   

    //往后翻

    if (x>=_viewWidth*2) {

        if (_currentPage==_imageViewAry.count-1) {

            _currentPage = 0;

        }else{

            _currentPage ++;

        }

    }

    [self reloadData];

}

#pragma mark 自动滚动

-(void)shouldAutoShow:(BOOL)shouldStart

{

    if (shouldStart)  //开启自动翻页

    {

        if (!_autoScrollTimer) {

            _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];

        }

    }

    else   //关闭自动翻页

    {

        if (_autoScrollTimer.isValid) {

            [_autoScrollTimer invalidate];

            _autoScrollTimer = nil;

        }

    }

}

#pragma mark 展示下一页

-(void)autoShowNextImage

{

    if (_currentPage == _imageViewAry.count-1) {

        _currentPage = 0;

    }else{

        _currentPage ++;

    }

    [self reloadData];

}

代码简单易懂,使用起来也很方便

//创建view view中包含UIScrollViewUIPageControl,设置frame

 _whView = [[WHScrollAndPageView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];

 //N张图片放到imageview

 NSMutableArray *tempAry = [NSMutableArray array];

 for (int i=1; i<NUM; i++) {

  UIImageView *imageView = [[UIImageView alloc] init];

  imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%i.jpg",i]];

  [tempAry addObject:imageView];

 }

 //imageView数组存到whView

 [_whView setImageViewAry:tempAry];

 //把图片展示的view加到当前页面

 [self.view addSubview:_whView];

 //开启自动翻页

 [_whView shouldAutoShow:YES];

 //遵守协议

 _whView.delegate = self;

 #pragma mark 协议里面方法,点击某一页

 -(void)didClickPage:(WHScrollAndPageView *)view atIndex:(NSInteger)index

 {

 NSLog(@"点击了第%li",index);

 }

 #pragma mark 界面消失的时候,停止自动滚动

 -(void)viewDidDisappear:(BOOL)animated

 {

 [_whView shouldAutoShow:NO];

 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值