如何在UIscrollView中以不同方式增加每个页面的高度

时间:2012-12-19 13:55:59

标签: iphone ios ipad cocoa-touch

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

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

先谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);
}

希望它有所帮助!