2016-02-28 106 views
1

我有一個允許用戶抓取單元的拖放切換語句,代碼可以無縫地創建它的快照,然後用戶可以將快照拖動到新的位置。繁榮!細胞移動。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) 
} 

注:細胞變化的高度,一些佔據屏幕高度的一半,其他的要小得多。

問題

此代碼的工作,但迅速回落卡表上/。快速到達桌面的頂部或底部非常好,但不適合逐漸滾動。有任何想法嗎?

有沒有辦法讓拖動的單元格靠近頂部時,表格以每秒固定的速率向上滾動?如果靠近底部,將以每秒固定速率向下滾動?

+0

您可以嘗試animateWithDuration減慢速度,效果較慢。 –

回答

0

嘗試通過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) 
} 
+0

這仍然是生澀的。這可能是因爲細胞高度不同,有的佔據屏幕高度的一半,有的則小得多。有沒有辦法讓拖動的單元格靠近頂部,表格以每秒固定的速率向上滾動?如果靠近底部,將以每秒固定速率向下滾動? –

+0

你覺得戴夫? –