简体   繁体   English

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

[英]UITableView get middle position via content offset

I want to add a little button in the bottom right corner of a UITableView. 我想在UITableView的右下角添加一个小按钮。 When we are in the top half of the tableview, the button programmatically scrolls you to the bottom, and when you are in the bottom half, it takes you to the top. 当我们位于表格视图的上半部分时,该按钮会以编程方式将您滚动到底部,而当您位于下半部分时,它将带您到顶部。 And the button's icon changes from "goto top" to "goto bottom" or vice versa depending on the situation. 并且按钮的图标根据情况从“转到顶部”变为“转到底部”,反之亦然。

In my code below, the button works fine- you press it when you are at the top, you hit the bottom, and the button graphic changes to the "goto top" icon. 在下面的代码中,该按钮可以正常工作-当您位于顶部时,按下按钮,然后单击底部,按钮图形将变为“转到顶部”图标。 However, dragging up and down like you would traditionally in a table doesn't make the button change it's icon properly. 但是,像传统上在表中那样上下拖动并不能正确更改按钮的图标。 You even need to pull past the border (top or bottom) of the table to make it flip to the correct state. 您甚至需要拉过表格的边框(顶部或底部),使其翻转到正确的状态。

When a button is pressed, if we are past halfway (as defined by a function pastHalfway), we go either to the top or bottom of the table. 当按下按钮时,如果我们过了一半(由函数pastHalfway定义),我们将转到表格的顶部或底部。 Something is wrong with it, but I've tussled a bit and basically made things not work well in various ways. 事情有问题,但是我有些打and,基本上以各种方式使事情无法正常进行。 I think the problem is I am not determining the midpoint content offset of the table correctly. 我认为问题是我无法正确确定表格的中点内容偏移量。

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

The necessary fixes were adding 1) 必要的修补程序添加了1)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM