swift简单学习之UITabbleView的简单应用(加动画效果)

本文介绍了在Swift中如何为UITableView的单元格添加动画效果。通过在`tableView:willDisplayCell:forRowAtIndexPath:`方法中设置`CATransform3D`变换,实现了单元格展示时的平滑旋转进入效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class ZHZOneViewController: ZHZBaseViewController,UITableViewDelegate,UITableViewDataSource {
     private var tabV:UITableView?
    
    
      lazy var dataArr:NSMutableArray = {
    
        var arr = NSMutableArray()
        
        for index in 1...100 {
              arr .addObject(NSString(format: "%d", index))
         }
        return arr
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initTab()
        setUpLeftBtn()
        setUpRightBtn()

       }
    
    private func setUpRightBtn(){
        let nextItem = UIBarButtonItem(title: "add", style: .Plain, target: self, action: "leftBtnClick")
        self.navigationItem.leftBarButtonItem = nextItem
        
    }
    private func setUpLeftBtn(){
        let nextItem = UIBarButtonItem(title: "编辑", style: .Plain, target: self, action:"rightBtnClick")
        self.navigationItem.rightBarButtonItem = nextItem
      }
      func rightBtnClick(){
          tabV!.setEditing(!tabV!.editing, animated: true)
        if (tabV!.editing == true ) {
            self.navigationItem.leftBarButtonItem!.enabled = true
            self.navigationItem.rightBarButtonItem!.title = "完成"
        }else{
            self.navigationItem.leftBarButtonItem!.enabled = false
            self.navigationItem.rightBarButtonItem!.title = "编辑"
         }
     }
     func leftBtnClick(){
        let row = self.dataArr.count
        let indexPath = NSIndexPath(forRow:row,inSection:0)
        self.dataArr.addObject("杭州")
        self.tabV?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
    }
    private func initTab(){
         tabV = UITableView(frame: self.view.frame, style: .Plain)
        tabV!.delegate = self;
        tabV!.dataSource = self;
        
        tabV!.setEditing(false, animated: false)
        view .addSubview(tabV!)
     }
    //总行数
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return dataArr.count
     }
    
    //加载数据
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let Id = "cellId"
        
        var cell = tableView.dequeueReusableCellWithIdentifier(Id)
        if cell == nil{
            cell = UITableViewCell(style: .Default, reuseIdentifier: Id)
        }
        cell?.textLabel?.text = dataArr[indexPath.row] as? String
        cell?.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
         return cell!
     }
    //选中行
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         tableView.deselectRowAtIndexPath(indexPath, animated: true)
         next()
     }
    func next(){
        let testVC = ZHZTestViewController()
        testVC.view.backgroundColor = UIColor.grayColor()
        self.navigationController? .pushViewController(testVC, animated: true)
      }
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
         let index = indexPath.row as Int
         self.dataArr.removeObjectAtIndex(index)
         self.tabV?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
     }
    
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.Insert
    }
    
    
    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        return "删除"

    }


  /**
     标题
     */
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.titles[section]
    }

    /**
     索引数组
     */
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return self.titles
    }

  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { let rotationTransform = CATransform3DTranslate(CATransform3DIdentity,200, 50, 0) cell.layer.transform = rotationTransform UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in cell.layer.transform = CATransform3DIdentity }, completion: nil) } 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值