我有一个疑问:是否有办法拦截
这种操作是否可行?!
谢谢。
tableView
的滚动并添加动作?例如,我的原型单元格背景是红色的,在单元格内向上滑动时,其背景颜色变为蓝色,而在滚动tableView
时,背景颜色变回红色。这种操作是否可行?!
谢谢。
tableView
的滚动并添加动作?例如,我的原型单元格背景是红色的,在单元格内向上滑动时,其背景颜色变为蓝色,而在滚动tableView
时,背景颜色变回红色。UITableView 继承自 UIScrollView,而 UITableViewDelegate 扩展了 UIScrollViewDelegate
。
您可能会对 scrollViewDidScroll 方法感兴趣。因此,在您的 UITableViewDelegate
实现中,添加以下方法:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
NSLog("Table view scroll detected at offset: %f", scrollView.contentOffset.y)
}
scrollViewDidEndDecelerating
。当用户完成拖动内容时,会调用scrollViewDidEndDragging
,但此后滚动仍可能继续移动。 - redent84CellForRow
是一种检测滚动的方式,同时还可以给出下一个进入视图的单元格的 indexPath.row
。如果用户滚动的距离很短,以至于新的或重复使用的单元格甚至没有配置,那么拖动将不会被检测到。然而,即使使用内置方法,例如scrollViewWillBeginDragging
,也无法检测到短距离滚动。
var lastCellForRowAtIndex = 0
var isScrollingUp = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
isScrollingUp = indexPath.row > lastCellForRowAtIndex
lastCellForRowAtIndex = indexPath.row
}
e.g:
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
}
}
最好保留一个变量,例如var isBackgroundColorRed: Bool = true
当您将背景颜色设置为蓝色时,另一个变量用于滚动y位置。例如:
var blueBackgroundColorYOffset: CGFloat?
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if !isBackgroundColorRed {
// apply some color blending between blue and red based on
// the amount of movement on y axis, until you reach the limit
// its also important to take the absolute value of the blueBackgroundColorYOffset
// and the offset here in scrollViewDidScroll to cover up or down movements
// say 1 cell's height, then set your isBackgroundColorRed to true
}
}
尝试将此内容添加到您的项目中,并更新您的桥接头文件。 UIColor-CrossFade
这种技术将为您提供良好的用户体验,而不是突然的背景变化。
cellForRowAtIndexPath
方法在你滚动 tableView 时会被重复调用,除非你的行数非常少。
UITableView
是否已经滚动?还是正在滚动? - Shamas S