美文网首页
关于UIScrollView广告栏的无限循环

关于UIScrollView广告栏的无限循环

作者: 这位网友 | 来源:发表于2016-07-26 11:04 被阅读88次

    虽然基本还是用到第三方,但是写一些简单的广告栏循环的话,还是要会写。知道基本的原理。

    本文写一个简单的Demo

    因为加载的图片基本都是数据库的图片,所以首先要有SDWebImage,然后添加头文件#import "UIImageView+WebCache.h"
    简单的写两个宏
    #define UIScreen_width ([UIScreen mainScreen].bounds.size.width)
    #define UIScreen_height ([UIScreen mainScreen].bounds.size.height)
    添加代理<UIScrollViewDelegate>
    然后定义变量

    @property (strong, nonatomic) NSMutableArray *imageArray;//存放图片的数组
    @property (strong, nonatomic) UIScrollView *ADScroll;//广告栏的底层ScrollView
    @property (strong, nonatomic) NSTimer *timer;//计时器
    

    开始在.m中完成简单的Demo

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _imageArray = [NSMutableArray array];
        [self createAD];
    }
    
    -(void)createAD{
        _ADScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, UIScreen_width, UIScreen_height/3)];
        _ADScroll.delegate = self;
        [self.view addSubview:_ADScroll];
        [self addImagesToScrollView];//添加图片
        [self createTimer];//创建计时器
    }
    
    -(void)addImagesToScrollView{
        //这里随便在网上搜了四张图片存放在图片数组中
        NSArray *arr = @[@"http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg",@"http://pic25.nipic.com/20121112/5955207_224247025000_2.jpg",@"http://img10.3lian.com/c1/newpic/10/08/04.jpg",@"http://img3.imgtn.bdimg.com/it/u=2699593702,2049257415&fm=206&gp=0.jpg"];
        [_imageArray addObjectsFromArray:arr];
        int i = 0;
        for (; i < _imageArray.count; i++) {
            UIImageView *img = [[UIImageView alloc] init];
            img.frame = CGRectMake(UIScreen_width * (i + 1), 50, UIScreen_width, UIScreen_height/3);
            [img sd_setImageWithURL:[NSURL URLWithString:arr[i]]];
            img.tag = i;
            [_ADScroll addSubview:img];
            
            img.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [img addGestureRecognizer:tap];
        }
        // 将最后一张图片弄到第一张的位置
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, UIScreen_width, UIScreen_height/3)];
        [img sd_setImageWithURL:[NSURL URLWithString:_imageArray[i-1]]];
        [_ADScroll addSubview:img];
        // 将第一张图片放到最后位置,造成视觉上的循环
        UIImageView *img0 = [[UIImageView alloc] initWithFrame:CGRectMake(UIScreen_width * (i + 1), 50, UIScreen_width, UIScreen_height/3)];
        [img0 sd_setImageWithURL:[NSURL URLWithString:_imageArray[0]]];
        [_ADScroll addSubview:img0];
        [_ADScroll setContentOffset:CGPointMake(UIScreen_width, 0)];//将起始位置设置在这里
        
        _ADScroll.pagingEnabled = YES;
        _ADScroll.scrollEnabled = YES;
        
        _ADScroll.showsHorizontalScrollIndicator = NO;
        _ADScroll.showsVerticalScrollIndicator = NO;
        _ADScroll.contentSize = CGSizeMake((i+2)*UIScreen_width, UIScreen_height/3);
    }
    
    #pragma mark - 给每一张imageView添加点击事件
    -(void)tapClick:(UITapGestureRecognizer *)tap{
        UIImageView *image = (UIImageView *)tap.view;
        int a = (int)image.tag;
        NSLog(@"a = %i ",a);
    //    NSLog(@"111111");
        
    }
    
    #pragma mark - NSTimer
    -(void)createTimer{
        if (!_timer) {
            _timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        }
    }
    
    -(void)timerRunning{
        
        float Offx = _ADScroll.contentOffset.x;
        Offx += UIScreen_width;
        [_ADScroll setContentOffset:CGPointMake(Offx, 0) animated:YES];
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        if (_ADScroll == scrollView) {
            //往左滑
            if (scrollView.contentOffset.x >= scrollView.contentSize.width - UIScreen_width ) {
                scrollView.contentOffset = CGPointMake(UIScreen_width, 0);
            }
            //往右滑
            if (scrollView.contentOffset.x <= 0) {
                scrollView.contentOffset = CGPointMake(UIScreen_width * _imageArray.count, 0); // 这里的4,是整个Image数组的个数。
            }
    //        if (page == 0) {
    //            [scrollView setContentOffset:CGPointMake(UIScreen_width * _imageArray.count, 0)];
    //
    //        }else if (page == _imageArray.count + 1){
    //            // 如果是第最后一页就跳转到数组第一个元素的地点
    //            [scrollView setContentOffset:CGPointMake(UIScreen_width, 0)];
    //        }
        }
    }
    
    -(void)dealloc{
        [_imageArray removeAllObjects];
        [_ADScroll removeFromSuperview];
        [_timer invalidate];
        _timer = nil;
    }
    

    上边就是一整个完整的Demo,不过是最简单的循环。
    如果想要看一些其他的可以看这里——UIScrollView(循环滚动图片)
    以及——iOS_UIScrollView实现无限滚动,思路与代码

    相关文章

      网友评论

          本文标题:关于UIScrollView广告栏的无限循环

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