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:

posted @ 2013-11-25 13:15  苹果吧  阅读(1942)  评论(0编辑  收藏  举报