我有一个UIScrollView,其宽度与其superview相同。它有一个非常宽的contentSize和水平滚动。
我正在尝试使用委托方法scrollViewWillEndDragging:withVelocity:targetContentOffset:将targetContentOffset-> x设置为负值(即,将内容区域的左边缘移动到更靠近屏幕的中心)。
设置值似乎有效(NSLog显示前后的更改)但scrollview似乎忽略了修改后的targetContentOffset,只是结束滚动为0。
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSLog(@"target: %@", NSStringFromCGPoint(*targetContentOffset));
if (targetContentOffset->x <= 0.0)
{
targetContentOffset->x = -300;
}
NSLog(@"target: %@", NSStringFromCGPoint(*targetContentOffset));
}
有人知道这是否可以使用这种方法完成,还是应该以其他方式进行?
我设法通过使用contentInset
属性来解决类似的问题。
这是Swift中的示例:
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Determine threshold for dragging to freeze content at the certain position
if scrollView.contentOffset.y < -50 {
// Save current drag offset to make smooth animation lately
var offsetY = scrollView.contentOffset.y
// Set top inset for the content to freeze at
scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
// Set total content offset to preserved value after dragging
scrollView.setContentOffset(CGPoint(x: 0, y: offsetY), animated: false)
// Make any async function you needed to
yourAsyncMethod(complete: {() -> Void in
// Set final top inset to zero
self.tripsTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
// Set total content offset to initial position
self.tripsTableView.setContentOffset(CGPoint(x: 0, y: -50), animated: false)
// Animate content offset to zero
self.tripsTableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
})
}
}
您可以改进它以用于水平滚动