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
复制代码

 

posted @   #零下一度&  阅读(313)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示