4

我有一个 UIScrollView,它的宽度与其父视图相同。它有一个非常宽的 contentSize 并且水平滚动。

我正在尝试使用委托方法 scrollViewWillEndDragging:withVelocity:targetContentOffset: 将 targetContentOffset->x 设置为负值(即将内容区域的左边缘移动到更靠近屏幕中心的位置)。

设置该值似乎有效(NSLog 显示之前和之后的更改)但滚动视图似乎忽略了修改后的 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));
}

有人知道这是否可以使用这种方法完成,还是我应该以其他方式完成?

4

1 回答 1

0

我已经设法通过使用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)
        })
    }
}

您可以改进它以用于水平滚动

于 2015-05-21T07:24:29.900 回答