ios - UITableView 通过内容偏移获取中间位置

标签 ios uitableview swift contentoffset

我想在 UITableView 的右下角添加一个小按钮。当我们位于 tableview 的上半部分时,按钮会以编程方式将您滚动到底部,而当您位于下半部分时,它将带您到顶部。按钮的图标会根据情况从“转到顶部”变为“转到底部”,反之亦然。

在我下面的代码中,按钮工作正常 - 当你在顶部时按下它,你点击底部,按钮图形变为“转到顶部”图标。然而,像传统上在表格中那样上下拖动并不能使按钮正确地改变它的图标。您甚至需要拉过表格的边框(顶部或底部)以使其翻转到正确的状态。

按下按钮时,如果我们已经过了一半(由函数 pastHalfway 定义),我们将到达表格的顶部或底部。它出了点问题,但我已经纠结了一下,基本上让事情在各种方面都不能很好地工作。我认为问题是我没有正确确定表格的中点内容偏移量。

func pastHalfway() -> Bool {
    // TODO: This doesn't work
    let offset:CGPoint = self.tableView.contentOffset
    let height:CGFloat = self.tableView.frame.height
    println("pastHalfway offset.y=\(offset.y) height=\(height)")
    return offset.y > (height/3.0) // 3.0 is 3/4 down the table
}


func gotoButtonPressed(sender: UIButton!) {
    if let realPost = post {
        if realPost.numberComments > 0 {
            if self.pastHalfway() {
                let indexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
                self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
            } else {
                let indexPath:NSIndexPath = NSIndexPath(forRow: realPost.numberComments - 1, inSection: 1)
                self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
            }
        }
    }
}

func maybeShowGotoButtons() {
    let yOffset:CGFloat = self.tableView.contentOffset.y

    if (self.post!.numberComments < minRowsToShowGotoButtons) {
        // Hide and disable buttons
        self.gotoButton.hidden = true
        self.gotoButton.enabled = false
    } else {
        // Show buttons, depending on content offset

        if self.pastHalfway() {
            self.gotoButton.setImage(UIImage(named: "gotoTopIcon"), forState: .Normal)
        } else {
            self.gotoButton.setImage(UIImage(named: "gotoBottomIcon"), forState: .Normal)
        }

        // And enable
        self.gotoButton.hidden = false
        self.gotoButton.enabled = true
    }
}

UIScrollView 委托(delegate)

override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    UIView.animateWithDuration(0.1, animations: {
        self.gotoButton.alpha = 0.5
    })
}

override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    UIView.animateWithDuration(0.2, animations: {
        self.gotoButton.alpha = 1.0
        self.maybeShowGotoButtons()
    })
}

override func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
    // This is used for the programmatic scroll top/bottom when clicking buttons
    self.maybeShowGotoButtons()
}

最佳答案

必要的修复正在添加 1)

override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    self.maybeShowGotoButtons()
}

2)

func atBottom() -> Bool {
    let height = self.tableView.contentSize.height - self.tableView.frame.size.height

    if self.tableView.contentOffset.y < 10 {
        //reach top
        return false
    }
    else if self.tableView.contentOffset.y < height/2.0 {
        //not top and not bottom
        return false
    }
    else {
        return true
    }
}

关于ios - UITableView 通过内容偏移获取中间位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26239411/

相关文章:

ios - iPhone Metal 的纵向和横向模式之间的奇怪差异

ios - 如何在 iOS 中同时执行两个 sqlite 查询?

ios - 按完成或uitextfield的返回键调用函数

ios - UITableView 中的 UITapGestureRecognizer - 第一个单元格被点击手势忽略

ios - 在 UITableViewController 中设置默认选中的单元格

ios - SVProgressHUD 代码乱序执行

swift - 在 Swift 中返回实例类型

ios - 如何为不同的设备缩放按钮

ios - 用选择器覆盖方法“tableview :cellForRowAtIndexPath

ios - 如何订购 Firebase 数据?