我有一个禁用分页的滚动视图。它有许多子视图。每个都有一个宽度约束,并相互约束和滚动视图,所以当子视图增长时,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)
此代码旨在:
不幸的是,它不仅没有滚动到正确的页面,似乎是因为它正在尝试计算在更新约束时滚动的数量,但它以一种不吸引人的时尚动画方式这样做。
如果我将我的代码更改为此(删除动画),它仍然无效:
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)
})
答案 0 :(得分:1)
也许解决方法是将contentOffset设置为动画块
UIView.animateWithDuration(0.35) {
self.view.layoutIfNeeded()
scrollView.setContentOffset(CGPoint(x: self.view.frame.width * CGFloat(page), y: 0), animated: false)
}