zoukankan      html  css  js  c++  java
  • UIScrollView 图片分页显示,这里用到了UIPageControl

    #import "MJViewController.h"

    #define kCount 8  //定义宏,程序中经常使用,方便扩展

    @interface MJViewController () <UIScrollViewDelegate>

    {

        UIPageControl *_pageControl;

    }

    @end

    @implementation MJViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

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

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

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

            UIImageView *imageView = [[UIImageView alloc] init];

            

            // 1.设置frame

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

            

            // 2.设置图片

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

            imageView.image = [UIImage imageNamed:imgName];

            

            [_scrollView addSubview:imageView];

        }

        

        // height == 0 代表 禁止垂直方向滚动

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

        _scrollView.showsHorizontalScrollIndicator = NO;

        _scrollView.pagingEnabled = YES;

        _scrollView.delegate = self;

        

        // 添加PageControl

        UIPageControl *pageControl = [[UIPageControl alloc] init];

        pageControl.center = CGPointMake(w * 0.5, h - 20);

        pageControl.bounds = CGRectMake(0, 0, 150, 50);

        pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)

        // 设置非选中页的圆点颜色

        pageControl.pageIndicatorTintColor = [UIColor redColor];

        // 设置选中页的圆点颜色

        pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

        

        // 禁止默认的点击功能

        pageControl.enabled = NO;

        

        [self.view addSubview:pageControl];

        _pageControl = pageControl;

    }

    #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;

    }

    @end

  • 相关阅读:
    使群辉支持NTFS(未完善)
    docker 解决 Dockerfile同级文件有其他文件 导致docker build包越来越大
    nginx location配置前后端地址
    前端 Umi框架自带的proxy功能请求后端地址
    linux常用命令
    arthas的使用(正常部署+服务docker部署)
    linux
    oracle行转列,列转行函数的使用(listagg,xmlagg)
    oracle 使用函数 ROW_NUMBER() OVER(PARTITION BY 列 ORDER BY 列 排序 ),自关联日志表,将列数据转换为 行数据
    oracle merge into用法
  • 原文地址:https://www.cnblogs.com/xgj0721/p/4338492.html
Copyright © 2011-2022 走看看