【问题标题】:In uiscrollviewdelegate, is it possible to set targetContentOffset to a negative value?在 uiscrollviewdelegate 中,是否可以将 targetContentOffset 设置为负值?
【发布时间】:2012-08-01 10:06:38
【问题描述】:

我有一个 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));
}

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

【问题讨论】:

  • 问这个问题的另一种方法是有没有办法告诉scrollView“弹回”到另一个地方?你试过 contentInset 吗?
  • @Killian:你找到解决问题的方法了吗?
  • stackoverflow.com/a/14589787/865175。 TL;DR,禁用 pagingEnabled 属性。

标签: ios5 uiscrollview uiscrollviewdelegate


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多