在Swift中逐行淡入UITableViewCell
我是新来的swift,我想有一个UITableView和单元格将被animation显示一个接一个。 我怎样才能做到这一点? 另外,如果新出现的一行单元格不在屏幕上(隐藏在桌子下面)。 每个单元格出现时如何移动表格?
var tableData1: [String] = ["1", "2", "3", "4", "5", "6", "7"] override func viewWillAppear(animated: Bool) { tableView.scrollEnabled=false tableView.alpha=0.0 NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "animateTable", userInfo: nil, repeats: false) } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.tableData1.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell cell.lblCarName.textAlignment = NSTextAlignment.Justified; return cell } func animateTable() { //what should be the code?// }
在UITableView中,这些行会在您进入“范围视图”时自动为您准备。 我假设你是,至less不是最初,能够滚动tableView,所以我们会滚动它编程使行出现,因为它走了。 我们怎么做? 其实UITableView有一个方法,让我们滚动它到一个特定的行:
scrollToRowAtIndexPath(indexPath : NSIndexPath, atScrollPosition : UITableViewScrollPosition, animated : Bool);
编写代码太累了,所以我会试着解释一下。 首先,对于每个单元格,您将alpha设置为0,只要它们被加载(cellForRowAtIndexPath)。 那么我们假设我们的屏幕适合5个第一行。 你要使用UIView.animateWithDuration(依次使用UIView.animateWithDuration)直到第五个(索引4),然后你将scrollInRowAtIndexPath传递给NSIndexPath,使用5,然后6,…直到其余部分(使用scrollPosition =。底部)。 对于他们每个人来说,一旦他们被装载,你就会动起来。 只记得在这个互动之间放一些时间。 (首先animation,运行NSTimer到第二个,然后继续)。 布尔animation当然应该是真的。
步骤1
在你的cellForRowAtIndexPath
方法初始化你的单元格,像这样隐藏;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell cell.lblCarName.textAlignment = NSTextAlignment.Justified cell.contentView.alpha = 0 return cell }
第2步
让我们做淡出animation。 UITableViewDelegate
具有willDisplayCell
方法,可以检测到当您滚动到顶部或底部时,第一个单元格将显示在窗口上。
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) { UIView.animateWithDuration(0.8, animations: { cell.contentView.alpha = 1.0 }) //Swift 3.0 /* UIView.animate(withDuration: 0.8, animations: { cell.contentView.alpha = 1.0 }) */ }
你的淡出animation正在进行中。 最重要的是,你不能直接在运行时设置你的单元格的alpha,因为iOS是作为你的UITableView
一部分进行一些特殊的内部处理,而忽略了你的设置。 所以,如果你设置你的单元的contentView
,你会是摇滚!
这里有一些代码可以让你开始。 在我的COBezierTableView我subclassed UITableView
并重写layoutSubviews
方法。 在那里你可以根据它们在视图中的相对位置来操纵单元格。 在这个例子中,我淡化了它们的底部。
import UIKit public class MyCustomTableView: UITableView { // MARK: - Layout public override func layoutSubviews() { super.layoutSubviews() let indexpaths = indexPathsForVisibleRows! let totalVisibleCells = indexpaths.count - 1 if totalVisibleCells <= 0 { return } for index in 0...totalVisibleCells { let indexPath = indexpaths[index] if let cell = cellForRowAtIndexPath(indexPath) { if let superView = superview { let point = convertPoint(cell.frame.origin, toView:superView) let pointScale = point.y / CGFloat(superView.bounds.size.height) cell.contentView.alpha = 1 - pointScale } } } } }
extension UITableView { func tvFadeCell() { self.reloadData() var delayCounter = 0.0 for cell in self.visibleCells { cell.alpha = 0.0 UIView.animate(withDuration: TimeInterval(delayCounter),animations: { cell.alpha = 1.0 }, completion: nil) delayCounter+ = 0.30 } } }
这个怎么样?