1
我想在UITableView的右下角添加一個小按鈕。當我們處於桌面視圖的上半部分時,按鈕將以編程方式將您滾動到底部,當您處於下半部時,它會將您帶到頂部。根據情況,按鈕的圖標從「goto top」變爲「goto bottom」,反之亦然。UITableView通過內容偏移獲得中間位置
在我的代碼如下,按鈕工作正常 - 當你在頂部,按下底部,按鈕圖形變爲「goto top」圖標時按下它。但是,像傳統上一樣在表格中上下拖動不會使按鈕正確更改圖標。您甚至需要拉過桌子的邊界(頂部或底部)以使其翻轉到正確的狀態。
當一個按鈕被按下時,如果我們過去了一半(由函數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()
}
是該相關? http://stackoverflow.com/a/26142136/388280 – ytbryan 2014-10-07 15:36:35
它是解決方案的一部分 – nflacco 2014-10-08 04:35:41