【问题标题】:Programmatically scroll UIScrollView from left to right an reverse以编程方式从左到右反向滚动 UIScrollView
【发布时间】:2013-04-17 20:20:11
【问题描述】:

我尝试从左到右自动滚动,但我还没有成功。 我正在评估以下表达式以更改方向(添加或减去像素)但是当滚动到达最终它被阻止...

(xContentSize - xOffset) >= CGRectGetWidth(_scroll.bounds)

(xContentSize - xOffset) > CGRectGetWidth(_scroll.bounds)

有什么想法吗?

【问题讨论】:

  • 你想指什么“它被阻止”?
  • sovannarith, 卷轴向右移动,但是当它到达final时,向左移动并无限期向右移动(被阻塞)
  • 你能告诉我你对滚动视图的计算吗?

标签: ios uiscrollview reverse right-to-left


【解决方案1】:

试试这个:

float w = scrollView.frame.size.width;
float h = scrollView.frame.size.height;
float newPosition = scrollView.contentOffset.x+w; 
CGRect toVisible = CGRectMake(newPosition, 0, w, h);

[scrollView scrollRectToVisible:toVisible animated:YES];

手动设置你想要的位置

【讨论】:

  • 我需要在哪里写这个。我的意思是在哪个滚动视图委托方法中。我在 scrollviewdidscroll 中写了但没有工作
【解决方案2】:

终于,我得到了解决方案。

每 4.0 秒使用一次 NSTimer

[UIView animateWithDuration:3.0
                     animations:^{

                         if (_direction == 0) {
                             _scroll.contentOffset = CGPointMake((_scroll.contentSize.width - CGRectGetWidth(_scroll.frame)), 0.0);
                         }else{
                             _scroll.contentOffset = CGPointMake(0.0, 0.0);
                         }

                     } completion:^(BOOL finished) {
                         _direction = !_direction;
                     }];

【讨论】:

    【解决方案3】:

    Swift 4 - 向右滚动 70 像素然后向左滚动(用于提示用户 UIScrollView 上有分页

    UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [.curveEaseInOut, .allowUserInteraction, .autoreverse], animations: {
      self.scrollView.contentOffset.x += 70
    }) { [unowned self] _ in
      self.scrollView.contentOffset.x = 0
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 2010-10-31
      • 2020-09-01
      • 1970-01-01
      相关资源
      最近更新 更多