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

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

当按下按钮时,如果我们过了一半(由函数pastHalfway定义),我们将转到表格的顶部或底部。事情有问题,但是我有些打and,基本上以各种方式使事情无法正常进行。我认为问题是我无法正确确定表格的中点内容偏移量。

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

最佳答案

必要的修复程序添加了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
    }
}

10-04 22:32