1

我在单个 uiscrollView 中有四个页面,并且启用了分页。每个页面可能有不同的高度,我试图在 scrollViewDidEndDecelerating 委托中增加滚动视图的内容大小,但这对我没有帮助。

任何人都可以建议如何以不同的方式增加每个页面中滚动视图的内容大小吗?

提前致谢。

4

2 回答 2

0

那是不可能的,内容大小是滚动视图的边界,是一个矩形,每个页面怎么会改变呢?为什么不缩放页面以使它们具有相同的大小并使用缩放?

于 2012-12-19T17:22:09.483 回答
0

我不认为你可以在本地做到这一点。但是,您可以尝试禁用分页并手动执行。

有一个有用的委托方法:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset;

这让您可以设置 scrollView 将在何处结束其滚动。

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    float offsetY = floorf((*targetContentOffset).y);
    float yGoto = 0.0;

    // Find out, based on that offsetY in which page you are
    // and set yGoto accordingly to the start of that page
    // In the following example my pages are 320px each

    // I start by only allowing to go 1 page at a time, so I limit
    // how far the offsetY can be from the current scrollView offset

    if(offsetY > scrollView.contentOffset.y + 160){
        // Trying to scroll to more than 1 page after
        offsetY = scrollView.contentOffset.y + 160;
    }
    if(offsetY < scrollView.contentOffset.y - 160){
        // Trying to scroll to more than 1 page before
        offsetY = scrollView.contentOffset.y - 160;
    }
    if(offsetY < 0){
        // Trying to scroll to less than the first element
        // This is related to the elastic effect
        offsetY = 0;
    }
    if(offsetY > scrollView.contentSize.height-320){
        // Trying to scroll to more than the last element
        // This is related to the elastic effect
        offsetY = scrollView.contentSize.height - 1;
    }

    // Lock it to offsets that are multiples of 320
    yGoto = floorf(offsetY);
    if((int)offsetY % 320 > 160){
        int dif = ((int)offsetY % 320);
        yGoto = offsetY + 320 - dif;
    }else{
        int dif = ((int)offsetY % 320);
        yGoto = offsetY - dif;
    }

    yGoto = floorf(yGoto); // I keep doing this to take out non integer part
    scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    (*targetContentOffset) = CGPointMake(scrollView.contentOffset.x,yGoto);
}

希望能帮助到你!

于 2012-12-19T17:54:59.230 回答