【问题标题】:UIPageControl + UIScrollView not counting pages properlyUIPageControl + UIScrollView 没有正确计算页面
【发布时间】:2014-09-03 01:48:21
【问题描述】:

我遇到了一个 UIPageControl 增加 currentPage 超过数组计数的问题 - 1。例如;如果数组中有 5 个项目,它实际上会绘制 5 个点,但它只会转到第 4 号。控件根据UIScrollView 的分页增加或减少,直到(我认为会是)给定数组中的最大项目数。任何帮助,将不胜感激。

这是scrollViewDidEndDecelerating的代码:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = _meatSelectionCollection.frame.size.width;
self.pageControl.currentPage = _meatSelectionCollection.contentOffset.x / pageWidth;
NSLog(@"Current Page Test: %ld", (long)self.pageControl.currentPage);
}

我正在检测 UIScrollView 中确定当前页面的页面宽度,并且启用了滚动视图中的分页。但是,当前页面的 NSLog 在页面更改时会读取以下输出:

2014-09-02 21:41:28.359 Burger Grinder[73531:60b] Current Page Test: 0
2014-09-02 21:41:29.420 Burger Grinder[73531:60b] Current Page Test: 1
2014-09-02 21:41:30.120 Burger Grinder[73531:60b] Current Page Test: 2
2014-09-02 21:41:30.870 Burger Grinder[73531:60b] Current Page Test: 3
2014-09-02 21:41:31.708 Burger Grinder[73531:60b] Current Page Test: 4
2014-09-02 21:41:32.476 Burger Grinder[73531:60b] Current Page Test: 4

它不会超过 4。确定它的数组计数返回为 6。我知道显示 4 的 self.page.currentPage 是计数 0, 1, 2, 3, 4 的结果(即 5 个元素),但计数是 6 .

对可能导致这种情况的任何想法?提前致谢。

编辑:在增量后显示内容偏移和页面宽度日志:

2014-09-02 22:03:52.472 Burger Grinder[73813:60b] Current Page Test: 1
2014-09-02 22:03:52.472 Burger Grinder[73813:60b] Content Offset.x Value: 244.000000
2014-09-02 22:03:52.473 Burger Grinder[73813:60b] PageWidth: 244.000000
2014-09-02 22:03:55.811 Burger Grinder[73813:60b] Current Page Test: 2
2014-09-02 22:03:55.811 Burger Grinder[73813:60b] Content Offset.x Value: 488.000000
2014-09-02 22:03:55.812 Burger Grinder[73813:60b] PageWidth: 244.000000

事实上,即使计数没有超过 4,Content Offset.x Value 也会增加。它会在最高级别页面正确地将值记录为 1213,但计数器不会增加。

【问题讨论】:

  • contentOffset.x和pageWidth的日志呢?
  • @trick14 已编辑以添加值。
  • 你确定你的scrollView有六页吗? scorollView.contentSize.width 的值是多少?
  • content.offset.x 在1213 处达到最大值,但scrollView.width 记录为1457。默认宽度为244、1457 / 244 = 6
  • 第1页的内容偏移x必须为0,page2-offset 244,page3-732,page4-976,page5-1220。 1213 来自哪里?如果内容偏移量为 1213,宽度为 244,1213 / 244 = 4.xxxx 表示页数为 4。

标签: objective-c uiscrollview uipagecontrol


【解决方案1】:

原来currentPage 的计算存在问题。在下面重新工具:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = _meatSelectionCollection.frame.size.width;
//    self.pageControl.currentPage = _meatSelectionCollection.contentOffset.x / pageWidth;

// This is where it needed to be calculated correctly rather then the line above it.

int page = floor((_meatSelectionCollection.contentOffset.x - pageWidth / 2) / pageWidth) + 1; 

self.pageControl.currentPage = page;


NSLog(@"Current Page Test: %ld", (long)self.pageControl.currentPage);
NSLog(@"Content Offset.x Value: %f", _meatSelectionCollection.contentOffset.x);
NSLog(@"PageWidth: %f", pageWidth);
NSLog(@"Scroll View: %f", scrollView.contentSize.width);
}

【讨论】:

    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多