zoukankan      html  css  js  c++  java
  • IOS开发之UIScrollVIew运用

    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 allocinitWithFrame:CGRectMake(index * self.scrollView.frame.size.width0,self.scrollView.frame.size.widthself.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:

  • 相关阅读:
    分页得到查询总数的方法 mysql
    input[type="file"] change事件第二次不触发
    小程序post请求,后台接收不到数据的解决方法
    Docker安装Kibana
    Docker安装ElasticSearch
    Docker安装Redis
    Docker安装Tomcat
    Docker安装 Nginx
    mysql服务设置远程连接 解决1251 client does not support ..问题
    Docker 安装MySQL容器
  • 原文地址:https://www.cnblogs.com/ios8/p/IOS-UIScrollVIew.html
Copyright © 2011-2022 走看看