博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS开发之UIScrollVIew运用
阅读量:6854 次
发布时间:2019-06-26

本文共 3636 字,大约阅读时间需要 12 分钟。

UIScrollView可以实现在一个界面看到所有内容,同时也不需要担心所显示的内容超出屏幕的大小,当超出之后可以翻阅至下一页浏览。

 

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {       //在UIScrollView滑动的时候调用此代理函数

    CGRect visibleBounds = scrollView.bounds;    //得到当前UIScrollView在屏幕中显示区域相对于scrollview的位置

    

    NSLog(@"%lf, %lf, %lf, %lf", visibleBounds.origin.x, visibleBounds.origin.y, visibleBounds.size.width, visibleBounds.size.height);

    NSLog(@"%lf, %lf", CGRectGetMinX(visibleBounds), CGRectGetMaxX(visibleBounds));

 

    

    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));

    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));

    NSLog(@"firsr:%d, last:%d", firstNeededPageIndex, lastNeededPageIndex);

    --firstNeededPageIndex;

    ++lastNeededPageIndex;

    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);

    lastNeededPageIndex  = MIN(lastNeededPageIndex, 7);

    NSLog(@"firsr:%d, last:%d", firstNeededPageIndex, lastNeededPageIndex);

 

 

/*

 

2012-03-16 14:22:01.531 skoda[3459:11903] 297.000000, 0.000000, 300.000000, 276.000000

2012-03-16 14:22:01.531 skoda[3459:11903] 297.000000, 597.000000  

CGRectGetMinX方法的作用得到目前scrollview在当前屏幕中相对于整个UIScrollView的最小值(位于屏幕的最左边)

CGRectGetMaxX方法的作用得到目前scrollview在当前屏幕中相对于整个UIScrollView的最大值(位于屏幕的最右边)

 

CGRectGetMinY方法的作用得到目前scrollview在当前屏幕中相对于整个UIScrollView的最小值(位于屏幕的最上边)

CGRectGetMaxY方法的作用得到目前scrollview在当前屏幕中相对于整个UIScrollView的最大值(位于屏幕的最下边)

 

CGRectGetMaxX-CGRectGetMinX)/2

CGRectGetMaxY-CGRectGetMinY)/2

 

 

 

2012-03-16 14:22:01.531 skoda[3459:11903] firsr:0, last:1 

2012-03-16 14:22:01.531 skoda[3459:11903] firsr:0, last:2

2012-03-16 14:22:01.547 skoda[3459:11903] 298.000000, 0.000000, 300.000000, 276.000000

2012-03-16 14:22:01.548 skoda[3459:11903] 298.000000, 598.000000

2012-03-16 14:22:01.548 skoda[3459:11903] firsr:0, last:1

2012-03-16 14:22:01.548 skoda[3459:11903] firsr:0, last:2

2012-03-16 14:22:01.564 skoda[3459:11903] 299.000000, 0.000000, 300.000000, 276.000000

2012-03-16 14:22:01.564 skoda[3459:11903] 299.000000, 599.000000

2012-03-16 14:22:01.564 skoda[3459:11903] firsr:0, last:1

2012-03-16 14:22:01.565 skoda[3459:11903] firsr:0, last:2

2012-03-16 14:22:01.581 skoda[3459:11903] 300.000000, 0.000000, 300.000000, 276.000000

2012-03-16 14:22:01.581 skoda[3459:11903] 300.000000, 600.000000

2012-03-16 14:22:01.581 skoda[3459:11903] firsr:1, last:1

2012-03-16 14:22:01.581 skoda[3459:11903] firsr:0, last:2

 

*/

 

    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {

        if (![self isDisplayingPageForIndex:index]) {

            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(index * self.scrollView.frame.size.width, 0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)];

            webView.backgroundColor = [UIColor clearColor];

            [self setCornerRadius:webView];

            [self.scrollView addSubview:webView];

            [self.visiblePages setObject:webView forKey:[NSNumber numberWithInt:index]];

            [webView release];

            

            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[kFileDirectoryPathstringByAppendingFormat:@"/service%d.html", index+1]]];

            [webView loadRequest:request];

        }

    }

属性:

contentOffset计算内容位移

contentInset表格外面得东西

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView   // 滚动停止时,触发该函数

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate   //触摸屏幕并拖拽画面,再松开,最后停止时,触发该函数

 

// 调用以下函数,来自动滚动到想要的位置,此过程中设置有动画效果,停止时,触发该函数

setContentOffset:animated: 

scrollRectToVisible:animated:

scrollToRowAtIndexPath:atScrollPosition:animated:

selectRowAtIndexPath:animated:scrollPosition:

scrollViewDidEndScrollingAnimation:

转载于:https://www.cnblogs.com/ios8/p/IOS-UIScrollVIew.html

你可能感兴趣的文章
Composer 中国镜像地址配置
查看>>
比特币暴跌后的连锁反应
查看>>
Python爬虫入门教程 62-100 30岁了,想找点文献提高自己,还被反爬了,Python搞起,反爬第2篇...
查看>>
第80节:Java中的MVC设计模式
查看>>
区块链100讲:以实例形式深入浅出讲透BANCOR算法
查看>>
Java并发编程 深入剖析volatile关键字
查看>>
Vue基础
查看>>
Flutter(一)之Flutter的的简单入门分析
查看>>
【Vue源码学习】Virtual Dom 原理解析
查看>>
js 中有哪些拷贝的方式
查看>>
k8s简单了解
查看>>
Quartz学习-通过binlog分析Quartz启动及集群的Failover
查看>>
当下流行的UI框架
查看>>
Python从零开始系列连载(21)——Python特色数据类型(元组)(下)
查看>>
[掘金专题] Google I/O 2017 已经结束,我们该如何评价?
查看>>
深入剖析Vue源码 - 选项合并(下)
查看>>
vue父、子、孙组件间数据传递、事件传递
查看>>
React 源码解析之总览
查看>>
Gulp
查看>>
Java比特币开发教程: 创建比特币钱包
查看>>