我有一个简单的PageViewController,我想根据从页面0滚动到页面1的百分比来更改顶部约束,即它开始于从超级视图顶部固定80像素,但是当我移至页面1时,我想要将其减少到40像素。
我已经将UIPageViewController子类化以公开滚动委托。我已经在一个简单的应用程序项目中进行了尝试,并获得了相同的结果,就像我尝试滑动时遇到的陪练经历,似乎与在调整约束的同时为pageviewcontroller设置动画感到困惑。
import UIKit
enum ScrollDirection {
case left
case right
}
class PSPageViewController: UIPageViewController, UIScrollViewDelegate {
weak var scrollDelegate: ScrollDelegate?
func scrollMoved(_ progress : CGFloat, _ direction: ScrollDirection)
{
if let scrollDelegate = scrollDelegate
{
scrollDelegate.PageViewScrolledDelegate(progress, direction)
}
}
override func viewDidLoad() {
super.viewDidLoad()
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
scrollView.delegate = self
}
}
// Do any additional setup after loading the view.
}
var lastOffset : CGFloat = 0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let point = scrollView.contentOffset
var direction = ScrollDirection.right
if (point.x < lastOffset)
{
direction = .left
}
lastOffset = point.x
// NSLog("x: %f", point.x)
// NSLog("width: %f", view.frame.size.width)
var percentComplete: CGFloat
percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
self.scrollMoved(percentComplete, direction)
}
}
当我启动PVC时,我使用此代码(使用示例项目)
self.pageViewController = PSPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
self.pageViewController!.delegate = self
self.pageViewController!.scrollDelegate = self
然后使用我自己的代理人
func PageViewScrolledDelegate(_ progress: CGFloat, _ direction: ScrollDirection) {
topConstraint.constant = 80 - (40 * progress)
print(progress)
}
有人遇到过同样的问题吗?如果可以,我该如何解决?
感谢您的所有帮助