• scrollView封装广告轮播示例代码


    #import <UIKit/UIKit.h>

    @interface PJXScrollView : UIScrollView

    -(void)setBannerImageArray:(NSArray*)imageArray;

    -(void)addPageControl;

    @end

    #import "PJXScrollView.h"

    #define SCROLL_VIEW_WIDTH  self.bounds.size.width

    #define SCROLL_VIEW_HEIGHT self.bounds.size.height

    #define SCROLL_VIEW_Y self.bounds.origin.y

    @interface PJXScrollView()<UIScrollViewDelegate>{

        UIImageView *_leftImageView;

        UIImageView *_centerImageView;

        UIImageView *_rightImageView;

        

        NSArray *_imageArray;

        

        CGFloat willBeginContentOffsetX;

        CGFloat willEndContentOffsetX;

        CGFloat endContentOffsetX;

        

        NSInteger _currentImageIndex;

        UIPageControl *_pageControl;

    }

    @end

    @implementation PJXScrollView

    -(instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self _initScrollViewStyle];

            [self _createImageView];

            

        }

        return self;

    }

    -(void)_initScrollViewStyle{

        //反弹效果

        self.bounces = NO;

        

        //设置滚动范围

        self.contentSize = CGSizeMake(SCROLL_VIEW_WIDTH*3,SCROLL_VIEW_HEIGHT );

    }

    -(void)_createImageView

    {

        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCROLL_VIEW_WIDTH*0, 0, SCROLL_VIEW_WIDTH, SCROLL_VIEW_HEIGHT)];

        [self addSubview:_leftImageView];

        

         _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCROLL_VIEW_WIDTH*1, 0, SCROLL_VIEW_WIDTH, SCROLL_VIEW_HEIGHT)];

        [self addSubview:_centerImageView];

        

        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCROLL_VIEW_WIDTH*2, 0, SCROLL_VIEW_WIDTH, SCROLL_VIEW_HEIGHT)];

        [self addSubview:_rightImageView];

    }

    -(void)setBannerImageArray:(NSArray*)imageArray

    {

        _imageArray = imageArray;

        _leftImageView.image = [UIImage imageNamed:_imageArray[0]];

        _centerImageView.image = [UIImage imageNamed:_imageArray[1]];

        _rightImageView.image  = [UIImage imageNamed:_imageArray[2]];

    }

    //监听滚动

    //手开始拖拽的时候调用,

    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {

        willBeginContentOffsetX = scrollView.contentOffset.x;

        

    }

    -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

        willEndContentOffsetX = scrollView.contentOffset.x;

    }

    //停止滚动时调用

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        endContentOffsetX = scrollView.contentOffset.x;

        

        //实现广告的循环播放

        if(willEndContentOffsetX > endContentOffsetX &&

           willEndContentOffsetX < willBeginContentOffsetX){

            

            //画面从左往右移动

            if (_currentImageIndex > 0) {

                _currentImageIndex--;

            }else{

                _currentImageIndex = _imageArray.count -1;

            }

            

        }else if(willEndContentOffsetX < endContentOffsetX &&

                 willEndContentOffsetX > willBeginContentOffsetX){

            //从右往左

            if (_currentImageIndex < _imageArray.count-1) {

                _currentImageIndex ++;

            }else{

                _currentImageIndex = 0;

            }

        }else{

            

        }

        //改变当前页码

        _pageControl.currentPage = _currentImageIndex;

        NSInteger leftPage = _currentImageIndex-1;

        if (leftPage < 0) {

            leftPage = _imageArray.count - 1;

        }

        

        NSInteger rightPage = _currentImageIndex + 1;

        if (rightPage >= _imageArray.count-1) {

            rightPage = 0;

        }

        

        //显示图片

        _leftImageView.image = [UIImage imageNamed:_imageArray[leftPage]];

        _centerImageView.image = [UIImage imageNamed:_imageArray[_currentImageIndex]];

        _rightImageView.image = [UIImage imageNamed:_imageArray[rightPage]];

        

        self.contentOffset = CGPointMake(SCROLL_VIEW_WIDTH, 0);

        

    }

    -(void)addPageControl{

        _pageControl = [[UIPageControl alloc] init];

        _pageControl.frame = CGRectMake(SCROLL_VIEW_WIDTH - 20*_imageArray.count, SCROLL_VIEW_Y+40, 20*_imageArray.count, 20);

        

        //设置页码数

        _pageControl.numberOfPages  = _imageArray.count;

        

        //设置当前页码数

        _pageControl.currentPage = 0;

        

        //设置能否能够点击

        _pageControl.enabled = NO;

        

        //设置颜色

        _pageControl.pageIndicatorTintColor = [UIColor redColor];

        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

        

        //绑定页码控件

        [self performSelector:@selector(bindPageControl) withObject:nil afterDelay:0.1f];

    }

    -(void)bindPageControl{

        [[self superview] addSubview:_pageControl];

    }

    @end

  • 相关阅读:
    hdu 2604 Queuing(矩阵快速幂乘法)
    hdu 5591 ZYB's Game
    hdu 5590 ZYB's Biology
    CodeForces
    uva 193 Graph Coloring(图染色 dfs回溯)
    uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
    CSU
    hdu 5115 Dire Wolf(区间dp)
    腾讯装扮下拉选项卡特效(QQ空间)
    楼梯式定位导航系统
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5898798.html
Copyright © 2020-2023  润新知