美文网首页ios开发记录
UIScrollowView 使用三个UIimageView实现

UIScrollowView 使用三个UIimageView实现

作者: selice | 来源:发表于2019-02-23 11:32 被阅读1次

    百度云地址链接 #密码:3noo


    需要调用的页面

    #import "ViewController.h"
    #import "SeliceInfininiteScrolloView.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //实现调用方法 将图片数组传入
        SeliceInfininiteScrolloView *scrollowView =  [[SeliceInfininiteScrolloView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 200)];
        scrollowView.imageArray  = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png"];
        [self.view addSubview:scrollowView];
    }
    
    @end
    
    

    自定义方法 SeliceInfininiteScrolloView.h

    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SeliceInfininiteScrolloView : UIView
    @property(nonatomic,strong) NSArray * imageArray;//图片数组
    
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    自定义方法 SeliceInfininiteScrolloView.m

    #import "SeliceInfininiteScrolloView.h"
    
    @interface SeliceInfininiteScrolloView()<UIScrollViewDelegate>{
        
        UIScrollView    *MainScrollowView;
        UIImageView     *leftImageView;
        UIImageView     *MidImageView;
        UIImageView     *RightImageView;
        NSTimer         *timer;
        UIPageControl   *pageControl;
        NSInteger       currentIndex;
        NSInteger       count;
        
    }
    
    @end
    
    
    
    @implementation SeliceInfininiteScrolloView
    
    static const int viewNumber  =  3;
    
    
    - (instancetype)initWithFrame:(CGRect)frame{
        
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor whiteColor];
            currentIndex = 0;
        }
        
        return self;
    }
    
    -(void)setImageArray:(NSArray *)imageArray{
        
        _imageArray = imageArray;
        count       = imageArray.count;
    
        //    新建UIScrollowView
        [self creatScrollowView];
        //    新建定时器
        [self creatTimer];
        //    新建UIPageControl
        [self creatPagecontrol];
        
    }
    
    
    #pragma mark  新建UIScrollowView
    -(void)creatScrollowView{
    
        //  当你滑动的时候,不滑动出scrollowView的时候 那么是不是就不会出现红色背景
        MainScrollowView = [[UIScrollView alloc]initWithFrame:self.bounds];
        MainScrollowView.pagingEnabled = YES;
        //   背景颜色的出现,本质是:滑动过界了,超过了scrollowView的contentsize所以才会出现背景色
        MainScrollowView.bounces = NO;
        
        MainScrollowView.backgroundColor = [UIColor redColor];
        MainScrollowView.showsVerticalScrollIndicator = NO;
        MainScrollowView.showsHorizontalScrollIndicator = NO;
        MainScrollowView.delegate = self;
        MainScrollowView.contentSize = CGSizeMake(viewNumber*self.frame.size.width, self.frame.size.height);
        [self addSubview:MainScrollowView];
        
    //   添加图片
        leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        leftImageView.image = [UIImage imageNamed:_imageArray[count-1]];
        
        
        MidImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        MidImageView.image = [UIImage imageNamed:_imageArray[0]];
    
        
        RightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        RightImageView.image = [UIImage imageNamed:_imageArray[1]];
    
        [MainScrollowView addSubview:leftImageView];
        [MainScrollowView addSubview:MidImageView];
        [MainScrollowView addSubview:RightImageView];
    
    //    刚刚开始的时候设置偏移量
        MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
        
    }
    
    #pragma mark  新建定时器
    -(void)creatTimer{
        
        __weak typeof(self)weakSelf = self;
        if (@available(iOS 10.0, *)) {
            timer = [NSTimer timerWithTimeInterval:2.f repeats:YES block:^(NSTimer * _Nonnull timer) {
                [weakSelf timerAction];
                
            }];
        } else {
            
        }
        
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    }
    
    
    -(void)timerAction{
        
        [MainScrollowView scrollRectToVisible:CGRectMake(2*self.frame.size.width, 0.f, self.frame.size.width, self.frame.size.height) animated:YES];
        
    }
    
    -(void)invalidateTimer{
        [timer invalidate];
        timer = nil;
    }
    
    
    #pragma mark  新建UIPageControl
    -(void)creatPagecontrol{
        
        CGFloat pageControlHeight = 20.f;
        CGFloat pageControlWidth  = 80.f;
        
        pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(20, self.frame.size.height-pageControlHeight, pageControlWidth, pageControlHeight)];
        pageControl.numberOfPages   = count;
        pageControl.currentPage     = 0.f;
        pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
        [self addSubview:pageControl];
        
    }
    
    
    #pragma mark  UIScrollView delegate method
    
    //手动滑动停止减速的时候调用
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        if (scrollView.contentOffset.x == 2 * self.frame.size.width) {
            //滑动到最右边时候
            currentIndex ++;
            //重置图片内容 修改偏移量
            [self resetImages];
        }else if (scrollView.contentOffset.x==0){
           
            currentIndex = currentIndex + count;
            currentIndex --;
            [self resetImages];
        }
    }
    
    
    //定时器滑动调用
    -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
     
        if (scrollView.contentOffset.x ==2*self.frame.size.width) {
            //滑动到最右边时候
            currentIndex ++;
            //重置图片内容 修改偏移量
            [self resetImages];
        }
    }
    
    #pragma mark  //重置图片内容 修改偏移量
    -(void)resetImages{
     
        leftImageView.image  = [UIImage imageNamed:_imageArray[(currentIndex-1)%count]];
        MidImageView.image   = [UIImage imageNamed:_imageArray[(currentIndex)%count]];
        RightImageView.image = [UIImage imageNamed:_imageArray[(currentIndex+1)%count]];
        MainScrollowView.contentOffset = CGPointMake(self.frame.size.width, 0.f);
        
        pageControl.currentPage = (currentIndex)%count;//设置page控件当前位置
        
    }
    
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
     
        //准备拖动的时候,定时器失效
        [self invalidateTimer];
        
    }
    
    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        
         //停止拖动的时候,定时器开启
        [self creatTimer];
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:UIScrollowView 使用三个UIimageView实现

        本文链接:https://www.haomeiwen.com/subject/genuyqtx.html