zoukankan      html  css  js  c++  java
  • UIScrollView自动轮播图

    添加定时器,自动播放图片列表

    #import "JZScrollViewController.h"

    @interface JZScrollViewController () <UIScrollViewDelegate>

    @property (nonatomic, strong) UIScrollView * scrollView;
    @property (nonatomic, strong) UIPageControl * pageControl;
    @property (nonatomic, strong) NSTimer * timer;

    @end

    @implementation JZScrollViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        [self initScroll];
        [self addImageWithtag:1000];
        [self initpageControl];
        
        [self addTime];
    }

    //初始化Scroll
    - (void)initScroll
    {
        UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.showsVerticalScrollIndicator = NO;
        scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, 0);
        scroll.pagingEnabled = YES;
        scroll.delegate = self;
        self.scrollView = scroll;
        [self.view addSubview:self.scrollView];
    }

    //初始化pageControl
    - (void)initpageControl
    {
        UIPageControl * page = [[UIPageControl alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 200) / 2, 300 - 30, 200, 30)];
        page.numberOfPages = 3;
        page.currentPage = 0;
        page.currentPageIndicatorTintColor = [UIColor blackColor];
        page.pageIndicatorTintColor = [UIColor greenColor];
        page.userInteractionEnabled = NO;
        
        self.pageControl = page;
        [self.view addSubview:page];
    }

    //添加图片
    - (void)addImageWithtag:(NSInteger) tag
    {
        for(int i = 0; i < 3; i++){
            NSString * name = [NSString stringWithFormat:@"%d.jpg", i + 1];
            UIImage * image = [UIImage imageNamed:name];
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 100, [UIScreen mainScreen].bounds.size.width, 200)];
            imageView.tag = tag + i;
            imageView.image = image;
            
            [self.scrollView addSubview:imageView];
        }
    }

    //滚动图片达到轮播图的效果
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGFloat scrollX = self.scrollView.contentOffset.x + [UIScreen mainScreen].bounds.size.width / 2;
        int num = (int)(scrollX / [UIScreen mainScreen].bounds.size.width);
        NSLog(@"%d", num);
        self.pageControl.currentPage = num;
    }

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        //关闭定时器
        [self removeTime];
    }

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        //打开定时器
        [self addTime];
    }

    //开启定时器
    - (void)addTime
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }

    //关闭定时器
    - (void)removeTime
    {
        [self.timer invalidate];
    }

    //显示下一张图片
    - (void)nextImage
    {
        int page = (int)self.pageControl.currentPage;
        if(page == 2){
            page = 0;
        }else{
            page++;
        }
        CGFloat X = page * self.scrollView.frame.size.width;
        self.scrollView.contentOffset = CGPointMake(X, 0);
    }

    @end

  • 相关阅读:
    java面试第八天
    java面试第七天
    java面试第六天
    java面试第五天
    java面试第四天
    SpringMVC导出Excel
    75. Autorelease机制及释放时机
    关于 SQLNET.AUTHENTICATION_SERVICES 验证方式的说明
    硬件十万个为什么——运放篇(五)PCB设计技巧
    eclipse到Android Studio的项目迁移
  • 原文地址:https://www.cnblogs.com/sjzlovecj/p/4662852.html
Copyright © 2011-2022 走看看