【发布时间】:2012-12-19 13:55:58
【问题描述】:
我在单个 uiscrollView 中有四个页面,并且启用了分页。每个页面可能有不同的高度,我尝试在 scrollViewDidEndDecelerating 委托中增加滚动视图的内容大小,但它对我没有帮助。
谁能建议如何在每个页面中以不同方式增加滚动视图的内容大小?
提前致谢。
【问题讨论】:
标签: iphone ios ipad cocoa-touch
我在单个 uiscrollView 中有四个页面,并且启用了分页。每个页面可能有不同的高度,我尝试在 scrollViewDidEndDecelerating 委托中增加滚动视图的内容大小,但它对我没有帮助。
谁能建议如何在每个页面中以不同方式增加滚动视图的内容大小?
提前致谢。
【问题讨论】:
标签: iphone ios ipad cocoa-touch
这不可能,内容大小是滚动视图的边界,是一个矩形,怎么可能每页都改变呢?为什么不缩放页面以使它们具有相同的大小并使用缩放功能?
【讨论】:
我不认为你可以在本地做到这一点。但是,您可以尝试禁用分页并手动执行。
有一个有用的委托方法:
-(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);
}
希望对你有帮助!
【讨论】: