Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Ios 调整scrollview的大小时滚动到scrollview页面_Ios_Swift_Uiscrollview - Fatal编程技术网

Ios 调整scrollview的大小时滚动到scrollview页面

Ios 调整scrollview的大小时滚动到scrollview页面,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我有一个禁用分页的滚动视图。它有许多子视图。每个视图都有一个宽度约束,并且彼此和scrollview都受到约束,因此当子视图增长时,scrollview内容区域也会增长 点击子视图时,我运行以下代码: var page = subView.tag for scrollViewSubViewWidthConstraint in scrollViewSubViewWidthConstraints { //this results in the subview being larger th

我有一个禁用分页的滚动视图。它有许多子视图。每个视图都有一个宽度约束,并且彼此和scrollview都受到约束,因此当子视图增长时,scrollview内容区域也会增长

点击子视图时,我运行以下代码:

var page = subView.tag

for scrollViewSubViewWidthConstraint in scrollViewSubViewWidthConstraints {
    //this results in the subview being larger than it previously was
    scrollViewSubViewWidthConstraint.constant = self.view.frame.width
}

UIView.animateWithDuration(0.35) {
    self.view.layoutIfNeeded()
}

scrollView.pagingEnabled = false
scrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(page), y: 0), animated: true)
本规范旨在:

  • 调整子视图的大小,使其成为“页面”的宽度
  • 在scrollview上启用分页
  • 平稳地滚动到正确的页面
  • 不幸的是,它不仅没有滚动到正确的页面,似乎是因为它试图在更新约束时计算滚动的数量,而且它以一种不吸引人的方式这样做

    如果我将代码更改为此(剪切动画),它仍然不起作用:

    self.view.layoutIfNeeded()
    
    scrollView.pagingEnabled = true
    scrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(page), y: 0), animated: false)
    
    这是可行的,但它不是动画,它是延迟的-这两者都不好

    self.view.layoutIfNeeded()
    
    UIHelper.delay(0.1, closure: { () -> () in //helper function that executes code after a given delay...
        self.scrollView.pagingEnabled = true
        scrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(page), y: 0), animated: true)
    })
    

    可能解决方法是将contentOffset设置到动画块中

        UIView.animateWithDuration(0.35) {
            self.view.layoutIfNeeded()
            scrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(page), y: 0), animated: false)
        }