zoukankan      html  css  js  c++  java
  • 滚动视图,这个好玩

    @property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置
    @property(nonatomic) CGSize contentSize; // 内容尺寸(能滚动的范围)
    @property(nonatomic) UIEdgeInsets contentInset; // 额外增加的滚动区域(在上下左右4个边缘)
    @property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 代理对象
    @property(nonatomic) BOOL bounces; // 是否有弹簧效果
    @property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否显示水平滚动条
    @property(nonatomic) BOOL showsVerticalScrollIndicator; // 是否显示垂直滚动条
    复制代码

    2.分页浏览的实现                             

    2.1.把需要显示的图片设置进UIScrollView                            

    复制代码
     1     CGFloat w = self.view.frame.size.width;
     2     CGFloat h = self.view.frame.size.height;
     3     for (int i = 0; i< kCount; i++) {
     4         UIImageView *imageView = [[UIImageView alloc] init];
     5         
     6         // 1.设置frame
     7         imageView.frame = CGRectMake(i * w, 0, w, h);
     8         
     9         // 2.设置图片
    10         NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + 1];
    11         imageView.image = [UIImage imageNamed:imgName];
    12         
    13         [_scrollView addSubview:imageView];
    复制代码

    2.2.设置UIScrollView的相关属性                            


    属性在文章的开头有介绍

     // height == 0 代表 禁止垂直方向滚动
        _scrollView.contentSize = CGSizeMake(kCount * w, 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;

    2.3.设置UIPageControl的相关属性,计算页码                            

    复制代码
     1     UIPageControl *pageControl = [[UIPageControl alloc] init];
     2     pageControl.center = CGPointMake(w * 0.5, h - 20);
     3     pageControl.bounds = CGRectMake(0, 0, 150, 50);
     4     pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)
     5     // 设置非选中页的圆点颜色
     6     pageControl.pageIndicatorTintColor = [UIColor redColor];
     7     // 设置选中页的圆点颜色
     8     pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
     9     
    10     // 禁止默认的点击功能
    11     pageControl.enabled = NO;
    12     
    13     [self.view addSubview:pageControl];
    14     _pageControl = pageControl;
    复制代码

    2.4.滚动时切换页码                                          

    复制代码
    #pragma mark - UIScrollView的代理方法
    #pragma mark 当scrollView正在滚动的时候调用
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    //    NSLog(@"%d", page);
        
        // 设置页码
        _pageControl.currentPage = page;
    }
  • 相关阅读:
    模拟两位选手进行n羽毛球比赛(15分赛制)并计算模拟胜率
    Pyton实例
    Python图片处理
    jieba库的使用和好玩的词云
    Python汉诺塔问题
    多线程同时操作一个epoll_fd
    Linux tr命令
    iptables 深入分析
    Linux xtables
    Linux IPC 共享内存
  • 原文地址:https://www.cnblogs.com/1995-08-29/p/4517328.html
Copyright © 2011-2022 走看看