Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIScrollView setContentOffset不正确_Swift_Uiscrollview - Fatal编程技术网

Swift UIScrollView setContentOffset不正确

Swift UIScrollView setContentOffset不正确,swift,uiscrollview,Swift,Uiscrollview,我有一个UIScrollView,里面有一些标签。我可以用一个按钮将滚动视图移动到另一个“页面”。但是当我把它推得太快时,偏移量就不对了 将滚动视图移动到下一页的我的代码: @IBAction func moveToRight(_ sender: Any) { let size = Int(scrView.contentOffset.x) + Int(scrView.frame.size.width); scrView.setContentOffset(CGPoint(x: si

我有一个UIScrollView,里面有一些标签。我可以用一个按钮将滚动视图移动到另一个“页面”。但是当我把它推得太快时,偏移量就不对了

将滚动视图移动到下一页的我的代码:

@IBAction func moveToRight(_ sender: Any) {
    let size = Int(scrView.contentOffset.x) + Int(scrView.frame.size.width);
    scrView.setContentOffset(CGPoint(x: size, y: 0), animated: true)
}
当我推得太快时,偏移量不正确。看起来当前动画停止并将从其当前(未完成)位置执行下一个动画


有人对此有解决方案吗?

我没有时间进行测试,但我认为您对问题主要原因的理解是正确的。由于
animated
设置为
true
,我猜
contentOffset.x
在动画完成之前尚未设置为其最终值

为什么不稍微更改一下逻辑并创建一个属性来记住上次滚动的当前页面:

var currentPage: Int = 0
然后,每次向右移动时,尽可能增加当前页码:

@IBAction func moveToRight(_ sender: Any) {
  let maxX = scrollView.contentSize.x - scrollView.frame.width
  let newX = CGFloat(currentPage + 1) * scrollView.frame.width
  if newX <= maxX {
    currentPage = currentPage + 1
    scrollView.setContentOffset(CGPoint(x: newX, y: 0), animated: true)
  }
}
@iAction func moveToRight(u发件人:任意){
设maxX=scrollView.contentSize.x-scrollView.frame.width
设newX=CGFloat(当前页面+1)*scrollView.frame.width
如果newX