Swift实现UITableViewCell的翻转效果
1>创建viewController.swift继承自UITableViewController 实现数据源方法
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CELLCOUNT // 默认返回5个cell
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// 在viewDidLoad方法中注册自定义的cell
// 注册自定义cell
//tableView.registerClass(DemoCell.self, forCellReuseIdentifier: identifier)
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! DemoCell
let model = self.data[indexPath.row] as!DemoModel
// 给cell的模型传值
cell.model = model
// 默认隐藏
cell.hidden = true
// 随机色
cell.backgroundColor = UIColor(red:CGFloat(arc4random_uniform(256)) / 255, green: CGFloat(arc4random_uniform(256)) / 255, blue: CGFloat(arc4random_uniform(256)) / 255, alpha: 1.0)
return cell
}
2>实现cell的didSelectRowAtIndexPath方法
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell:DemoCell = tableView.cellForRowAtIndexPath(indexPath) as! DemoCell
if index > CELLCOUNT - 1 && index < data.count{
cell.model = data[index] as? DemoModel
}else{
cell.model = data[index % (data.count)] as? DemoModel
}
cell.backgroundColor = UIColor(red:CGFloat(arc4random_uniform(256)) / 255, green: CGFloat(arc4random_uniform(256)) / 255, blue: CGFloat(arc4random_uniform(256)) / 255, alpha: 1.0)
// 设置动画
let transition = CATransition()
// 动画的类型
transition.type = "cube"
// 动画的时间
transition.duration = 0.7
cell.layer.addAnimation(transition, forKey: nil)
// 显示
cell.hidden = false
}
3>通过定时器执行didSelectRowAtIndexPath方法
// 添加定时器
func addTimer () {
let t = NSTimer.scheduledTimerWithTimeInterval(2.5, target: self, selector: "run", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(t, forMode: NSRunLoopCommonModes)
}
// 定时器执行的方法
func run () {
index++
var row = index
if row > CELLCOUNT - 1 {
row = index % CELLCOUNT
}
let indexPath = NSIndexPath.init(forRow: row, inSection: 0)
tableView(tableView, didSelectRowAtIndexPath: indexPath)
}
4>懒加载数据
// 加载数据
lazy var data:NSMutableArray = {
let arr = NSMutableArray()
let filePath = NSBundle.mainBundle().pathForResource("demo.plist", ofType: nil)
let dict = NSDictionary(contentsOfFile: filePath!)
let tmp: NSArray = dict?.objectForKey("Info") as! NSArray
for d in tmp{
let model = DemoModel(dict: d as! [String : AnyObject])
arr.addObject(model)
}
return arr
}()
效果图:
本文项目代码地址:https://github.com/RuningFish/turnDemo.git