1

我想在 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 代表

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()
}
4

1 回答 1

1

必要的修复是添加 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
    }
}
于 2014-10-08T04:37:56.243 回答