UITableView通过内容偏移获得中间位置

时间:2014-10-07 15:16:33

标签: 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代表

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 个答案:

答案 0 :(得分: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
    }
}