我有两个具有垂直滚动的UIScrollViews:foreground
和background
。用户只能与前者互动;后者以编程方式移动。如何使用户滚动foreground
时background
以1/4的比例滚动?例如,对于foreground
滚动的每4px,background
将沿相同方向滚动1px。
如何在Swift2中实现这种关系?
最佳答案
将自己设置为scrollView委托-
self.foregroundScrollView.delegate = self
并使用UIScrollViewDelegate方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastY = scrollView.contentOffset.y;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Calculate how much distance the scrollView has travelled since last scroll
CGFloat currentY = scrollView.contentOffset.y;
CGFloat difference = currentY - self.lastY;
// Set new contentOffset for your background scrollView
CGPoint currentBackgroundOffset = self.backgroundScrollView.contentOffset;
currentBackgroundOffset.y += difference/4;
self.backgroundScrollView.contentOffset = currentBackgroundOffset;
// Don't forget to update the lastY
self.lastY = currentY;
}
迅速:
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
lastY = scrollView.contentOffset.y
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// Calculate how much distance the scrollView has travelled since last scroll
let currentY = scrollView.contentOffset.y
let difference = currentY - lastY
// Set new contentOffset for your background scrollView
var currentBackgroundOffset = backgroundScrollView.contentOffset
currentBackgroundOffset.y += difference/4
backgroundScrollView.contentOffset = currentBackgroundOffset
// Don't forget to update the lastY
lastY = currentY
}
关于ios - 将一个UIScrollView按比例滚动到另一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34108641/