zoukankan      html  css  js  c++  java
  • IOS ——UI篇—— UIPageControl的使用以及与UIScrollView的结合

    UIPageControl是分页符,在新闻类的APP中很常见,随着新闻页面的滚动,在屏幕中会有一些小点,也随着移动,用来分辨当前的页数,就是UIPageControl和ScrollView的结合使用;

    直接给大家上代码,这样更有助于理解:

     1  page = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 30, 200, 40)];
     2     page.backgroundColor = [UIColor clearColor];
     3     page.numberOfPages = 3;//设置页数(多少个点)
     4     page.currentPage = 0;//设置当前选中页
     5     NSLog(@"%d",page.currentPage);//获取当前选中页下标
     6     page.pageIndicatorTintColor = [UIColor greenColor];//未选中颜色
     7     page.currentPageIndicatorTintColor = [UIColor redColor];//当前选中的颜色
     8     [page addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
     9     [self.view addSubview:page];
    10 
    11 UIPageControl与UIScrollView 的结合使用
    12 
    13 #import "ViewController.h"
    14 
    15 @interface ViewController ()<UIScrollViewDelegate>{
    16     UIPageControl *page;
    17     UIScrollView  *scrollView;
    18 }
    19 
    20 @end
    21 
    22 @implementation ViewController
    23 
    24 - (void)viewDidLoad {
    25     [super viewDidLoad];
    26 
    27     page = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 30, 200, 40)];
    28     page.backgroundColor = [UIColor clearColor];
    29     page.numberOfPages = 3;//设置页数(多少个点)
    30     page.currentPage = 0;//设置当前选中页
    31     NSLog(@"%d",page.currentPage);//获取当前选中页下标
    32     page.pageIndicatorTintColor = [UIColor greenColor];//未选中颜色
    33     page.currentPageIndicatorTintColor = [UIColor redColor];//当前选中的颜色
    34     [page addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    35     [self.view addSubview:page];
    36 
    37     scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 90, 300, 400)];
    38     scrollView.contentSize = CGSizeMake(900, 0);
    39     scrollView.delegate = self;
    40     scrollView.pagingEnabled = YES;
    41     [self.view addSubview:scrollView];
    42 
    43     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    44     view.backgroundColor = [UIColor grayColor];
    45     [scrollView addSubview:view];
    46 
    47     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 300, 400)];
    48     view1.backgroundColor = [UIColor brownColor];
    49     [scrollView addSubview:view1];
    50 
    51     UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(600, 0, 300, 400)];
    52     view2.backgroundColor = [UIColor purpleColor];
    53     [scrollView addSubview:view2];
    54 
    55 
    56 }
    57 
    58 -(void)change:(id)page{
    59     NSLog(@"--%zi",[page currentPage]);
    60 
    61CGPoint p = {[page currentPage]*300,0};//获取分页符当前的页数与view的宽相乘作为CGPoint的x
    
    
    62  [scrollView setContentOffset:p animated:YES];//将p赋给scrollView的当前滚动位置,就建立了联系

    63 }
    64

    65

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    66 int index = scrollView.contentOffset.x/scrollView.frame.size.width;//当scrollView滚动停止时,将scrollView当前滚动到的位置除以view的宽,获得的值就是页数

    67 page.currentPage = index; //将上面获得的页数赋给page,就是page的当前页

    68 }

    69

    70 - (void)didReceiveMemoryWarning {
    71 [super didReceiveMemoryWarning];
    72 // Dispose of any resources that can be recreated.

    73 }
    74

    75 @end
    感谢您的访问! 若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
  • 相关阅读:
    241. Different Ways to Add Parentheses java solutions
    89. Gray Code java solutions
    367. Valid Perfect Square java solutions
    46. Permutations java solutions
    116. Populating Next Right Pointers in Each Node java solutions
    153. Find Minimum in Rotated Sorted Array java solutions
    判断两颗树是否相同
    求二叉树叶子节点的个数
    求二叉树第k层的结点个数
    将二叉排序树转换成排序的双向链表
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/4992540.html
Copyright © 2011-2022 走看看