Swift实现UITableViewCell的翻转效果

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值