我正在使用Apple的PageControl sample code进行纵向布局的水平滚动,一切正常,除非首次加载UIScrollView时,显示的初始页面有
1) The first page offset by 11 pixels at location (11,0) or
2) The second page offset by 19 pixels at location (301,0) or
3) The second page offset by 65 pixels at location (255,0) or
4) The first page offset by 81 pixels at location (81,0) or...
我的Xcode项目的整体结构是:
UITabBarController -> UITableView -> UIViewController containing UIScrollView
即。选择UITabBarController上的选项卡会显示UITableView。在UITableView中选择一个单元格会显示一个包含UIScrollView的视图。首次显示此UIScrollView时会发生偏移问题。
当UIScrollView首次加载时,似乎多次调用scrollViewDidScroll(34!),而在Apple的PageControl示例代码中,首次加载UIScrollView时不会调用scrollViewDidScroll。
UIScrollView加载后,UIScrollView工作正常,滚动结束时帧边界上的页面为320的倍数。我的UIScrollview框架的宽度等于页面的数量乘以320.我在UIView里面有UIScrollView。
这个post的答案是每次滚动边界改变时都会调用scrollViewDidScroll,但据我所知,我没有改变滚动边界;我为每个加载到UIScrollView中的viewControllers保持帧大小相同。
我在Apple的PageControl示例代码中使用viewDidLoad而不是awakeFromNib。有谁知道可能导致什么:
A) scrollViewDidScroll to get called multiple times when UIScrollView first loads?
B) why scrollViewDidScroll would end on offsets that are not multiples of 320?
C) is the problem that UITableView contains a UIScrollView and so when transitioning
from UITableVIew to UIScrollView some parameters in the UITableView are being
used by the UIScrollView?
答案 0 :(得分:0)
每个滚动移动的scrollview边界都会发生变化。边界基本上是scrollview中当前可见的矩形。
示例:
框架:{0,0,320.0,480.0} contentSize:{320.0,1000.0} contentOffset:{0,100.0}
界限将是:{0,100.0,320.0,480.0}
因此,对于contentOffset的每次更改,边界都会发生变化。这会导致在滚动时连续调用layoutSubviews和didScroll。
答案 1 :(得分:0)
我找到了解决方案。问题是在loadScrollViewWithPage中我将ViewController添加到UIScrollView然后修改ViewController。通过在完成修改控制器后将ViewController的添加移动到loadScrollViewWithPage的末尾,问题就消失了。 所以我将以下行移动到loadScrollViewWithPage的末尾:
[self.scrollView addSubview:controller.view];
答案 2 :(得分:0)
让我告诉你:
对于A)没错,当UIScrollView时,scrollViewDidScroll可能被调用超过2次 首先加载。
对于B)1:如果它以不是320的倍数的偏移结束,你可能已经忘记了或者 设置错误的帧和内容变量,你应该这样做:
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width * testArray.count
,myScrollView.frame.size.height/2);
myScrollView.contentOffset = CGPointMake(0, myScrollView.frame.origin.y);
2: Then for the custom view you want to add into scrollview, you should set its frame like:
Customview : CGRectMake(n*myScrollView.frame.size.with, 0, myScrollView.frame.size.with,
myScrollView.frame.size.height)];
That's to say, when the width of custom view is same with your scrollview, you will get the effect that you want.
对于C)那是不对的,忘了。