问题描述
我在Xcode 6上快速处理UITableView时遇到了麻烦:
I'm having troubles with my UITableView in swift on Xcode 6 :
我希望我的UITableView单元格在出现/消失时淡入/淡出.
I would like my UITableView cells to fade in / out when appearing / disappearing.
我在网上看了很多淡入淡出的动画,但是我没有找到想要的东西,因为它是基于持续时间动画的.
I looked a lot on the web for fade animations but I didn't find what I wanted because it's based on duration animations.
我认为我需要的是一种基于alpha的蒙版?我不是苏尔,我什至都不知道必须创建一个...
I Think what I need is sort of mask based on alpha ? I'm not sur and I don't even know have to create one...
我有一个UIViewController,其中包含一个tableView和UITableView的上下左右的空白(目前).
I have a UIViewController containing a tableView and some blank space (for now) upside and downside of the UITableView.
谢谢您的帮助!
最好
Anatole
推荐答案
animateWithDuration似乎是适当的,因为淡入/淡出具有持续时间:该单元随着时间逐渐变得可见或不可见.
animateWithDuration seems appropriate, since the fade in/out has a duration: the cell gradually becoming visible or invisible over time.
以下是使表格单元淡入的示例:
Here is an example of making table cells fade-in:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
cell.backgroundColor = UIColor.grayColor()
cell.alpha = 0
UIView.animateWithDuration(2, animations: { cell.alpha = 1 })
return cell
}
这将根据其y位置设置单元格的alpha,这可能更接近于您想要的位置:
This sets the cell's alpha based on their y position, which is probably closer to what you want:
func scrollViewDidScroll(scrollView: UIScrollView) {
for cell in tableView.visibleCells() as [UITableViewCell] {
var point = tableView.convertPoint(cell.center, toView: tableView.superview)
cell.alpha = ((point.y * 100) / tableView.bounds.maxY) / 100
}
}
这篇关于Swift-UITableView中的单元格褪色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!