zoukankan      html  css  js  c++  java
  • 使用UIScrollView 实现分页功能

    首先找8张jpg的图片,将它的名字分别设置为01.png......08.jpg,然后将代码运行,就可看见效果

    - (void)viewDidLoad

    {

        [superviewDidLoad];

        _scrollView = [[UIScrollViewalloc]init];

        //设置scrollview的可视范围

        _scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

        //1.创建UIImageView用来显示在UIScrollView

            CGFloat w = self.view.frame.size.width;

        CGFloat h = self.view.frame.size.height;

        for (int i = 0; i < 9; i++) {

            NSString *imageName = [NSStringstringWithFormat:@"0%d.jpg",i+1];

            UIImage *image = [UIImage imageNamed:imageName];

            UIImageView *imageView = [[UIImageViewalloc]initWithImage:image];

            //2.设置imageview的frame

            imageView.frame = CGRectMake(i * w, 0, w, h);

            [_scrollView addSubview:imageView];

        }

        //3.设置scrollview的内容尺寸

        _scrollView.contentSize = CGSizeMake(w * 8, 0);

        _scrollView.delegate = self;

        _scrollView.pagingEnabled = YES;

              [self.viewaddSubview:_scrollView];

        //4.添加pagecontrol

        UIPageControl *pageControl = [[UIPageControlalloc]init];

        pageControl.currentPageIndicatorTintColor = [UIColorredColor];

        pageControl.pageIndicatorTintColor = [UIColorblackColor];

        pageControl.center = CGPointMake(160, 440);

        pageControl.bounds = CGRectMake(0, 0, 80, 20);

        //5.如果不设置总页数,那么就不会显示pagecontrol

        pageControl.numberOfPages = 8;

        //6.禁止默认的点击功能

        pageControl.enabled = NO;

        [self.view addSubview:pageControl];

        _pageControl = pageControl;

    #pragma mark - scrollview的代理方法,此方法在拖动scrollview时就会调用

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        //7.设置pagecontrol的当前值,它的当前值也就是scrollviewcontentoffsetx除以scrollview的宽度,

        int page = _scrollView.contentOffset.x / scrollView.frame.size.width;

        _pageControl.currentPage = page;

    }

  • 相关阅读:
    解决MVC的Controller和Web API的Controller类名不能相同的问题
    SSO的踢人模式/禁入模式
    os处理文件
    pyton mock
    python xml转换成json
    python调用jpype
    Python程序的执行过程原理(解释型语言和编译型语言)
    python 字典、列表、字符串 之间的转换
    python接口自动化1
    不用框架,原生使用python做注册接口/登陆接口/充值接口的测试,做的数据/代码分离
  • 原文地址:https://www.cnblogs.com/yinqiang/p/3446710.html
Copyright © 2011-2022 走看看