这不是一个严格的编程问题,而是更多的“如何实现这个”问题。
我很好奇(并且正在研究可能需要这个的应用程序)如何实现左右视差滚动。要确切了解我的意思,请查看Yahoo Weather应用程序(它是免费的 - 不用担心)。
他们是否仅为每个显示的视图使用一个视图控制器或单独的控制器? 实现这个的最简单方法是什么?我发现this topic here有点解释了一下,但他们什么时候从服务器获取信息?是在更改视图时还是在应用程序启动时?
非常感谢任何有关如何实现此类滚动的信息。
答案 0 :(得分:5)
实际上非常简单:
就是这样!
我自己创建了一个多功能的UIScrollView子类,它非常简单易用,非常适合这种情况!在GitHub上获取它:https://github.com/LeonardPauli/LPParallaxScrollView
祝你好运!
答案 1 :(得分:0)
我基于标准的UIPageViewController子类和用于视差效果的自动布局约束实现了一个小型原型。 如果您想看一下,它已经有完整的记录:TestParallax
简而言之,视差的核心是通过scrollViewDidScroll
委托方法完成的:
extension PageViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let screenWidth = scrollView.bounds.width
/*
In a UIPageViewController, the initial contentOffset.x is not 0, but equal to the screen width
(so that the offset goes between (1 * screenWidth) and 0 when going to the previous view controller,
and from (1 * screenWidth) to (2 * screenWidth) when going to the next view controller).
Also, it's reset to the screenWidth when the scroll to a previous or next view controller is complete.
Therefore, we calculate a new 'horizontalOffset' starting at 0, and going:
- negative from 0 to (-screenWidth/2) when scrolling to the next view controller,
- and from 0 to (screenWidth/2) when scrolling to the previous view controller.
*/
let horizontalOffset = (scrollView.contentOffset.x - screenWidth)/2
// Special case: initial situation, or when the horizontalOffset is reset to 0 by the UIPageViewController.
guard horizontalOffset != 0 else {
previousPageController?.offsetBackgroundImage(by: screenWidth/2)
currentPageController?.offsetBackgroundImage(by: 0)
nextPageController?.offsetBackgroundImage(by: -screenWidth/2)
return
}
// The background image of the current page controller should always be offset by the horizontalOffset (which may be positive or negative)
guard let currentPageController = currentPageController else { return }
currentPageController.offsetBackgroundImage(by: horizontalOffset)
if horizontalOffset > 0 { // swiping left, to the next page controller
// The background image of the next page controller starts with an initial offset of (-screenWidth/2), then we apply the (positive) horizontalOffset
if let nextPageController = nextPageController {
let nextOffset = -screenWidth/2 + horizontalOffset
nextPageController.offsetBackgroundImage(by: nextOffset)
}
} else { // swiping right, to the previous page controller
// The background image of the previous page controller starts with an initial offset of (+screenWidth/2), then we apply the (negative) horizontalOffset
if let previousPageController = previousPageController {
let previousOffset = screenWidth/2 + horizontalOffset
previousPageController.offsetBackgroundImage(by: previousOffset)
}
}
}
}
答案 2 :(得分:0)
您不应更改页面视图控制器的滚动视图的委托。它可以破坏其正常行为。
相反,您可以:
向页面视图控制器的视图添加平移手势:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panRecognized(gesture:)))
view.addGestureRecognizer(panGesture)
panGesture.delegate = self
添加新功能以了解视图如何滚动。
@objc func panRecognized(gesture: UIPanGestureRecognizer) {
// Do whatever you need with the gesture.translation(in: view)
}
将您的ViewController声明为UIGestureRecognizerDelegate
。
实现此功能:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}