[英]Smoother Auto-Scrolling in UITableView
我有一個拖放開關語句,該語句允許用戶抓取一個單元格,代碼無縫地創建它的快照,然后用戶可以將快照拖動到新位置。 繁榮! 細胞移動了。
當行/單元格的數量比iPhone屏幕長時,我添加了代碼以允許用戶向上/向下拖動表格。
if locationOnScreen.y < 200 && indexPath!.row > 1 {
let indexPath = NSIndexPath(forRow: indexPath!.row-1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
} else if locationOnScreen.y > 550 && indexPath!.row < listCount {
let indexPath = NSIndexPath(forRow: indexPath!.row+1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
}
注意:單元格的高度各不相同,某些單元格占據屏幕高度的一半,另一些則小得多。
題
該代碼有效,但是可以快速向上/向下對齊表。 它非常適合快速到達表格的頂部或底部,但不適用於逐步滾動。 有任何想法嗎?
有沒有一種方法可以使如果拖動的單元格位於頂部附近,則表格以每秒固定的速率向上滾動? 如果接近底部,是否會以每秒固定的速率向下滾動?
嘗試傳遞animated: true
。 它應該滾動到帶有動畫的單元格
if locationOnScreen.y < 200 && indexPath!.row > 1 {
let indexPath = NSIndexPath(forRow: indexPath!.row-1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
} else if locationOnScreen.y > 550 && indexPath!.row < listCount {
let indexPath = NSIndexPath(forRow: indexPath!.row+1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.