美文网首页
致那些复杂的响应式框架5 全新移动端框架 CatFlow

致那些复杂的响应式框架5 全新移动端框架 CatFlow

作者: wangyongyue | 来源:发表于2019-05-08 22:32 被阅读0次

写东西可以写的不好,不好可以改,可以写的不深,慢慢就会好
但是不能半途而废,尤其发现好的东西,一定要让大家知道的。
CatFlow 轻量级,观察者模式的响应式框架,借鉴MVVM模式优化。
https://github.com/wangyongyue/CatFlow.git
优点
1,上手简单,几乎不需要学习时间
2,对现有代码侵入低,拷贝即可使用
3,原生代码封装,没有使用runtime,不存在性能问题

先上一个使用案例
ViewController

class MainVC: CViewController {

    //实例一个Observe,对数组的观察者
    var obArray = Observe()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white
        
       
        let table = CTable()
        self.view.addSubview(table)
       //绑定数组观察者
        table.v_array(ob: obArray)
        table.frame = CGRect.init(x: 0, y: 100, width: Screen.width(), height: Screen.height() - 200)
        table.register([MainTemplate.classForCoder()])
        
        let button = CButton()
        button.setTitle("button", for: .normal)
        button.setTitleColor(UIColor.red, for: .normal)
        self.view.addSubview(button)
        button.frame = CGRect.init(x: 100, y: Screen.height() - 100, width:100, height: 30)

        
        let m = Main()
        button.v_click {
            
            m.loadData(ob: self.obArray)
            
        }
        
        
        table.v_didSelect { (index) in

            print(index)
            Router.push(MainDetailsVC(), ["id":10], { (obj) in
                print(obj)
            })
        }
        
        table.v_didEvent { (model) in
        
            let a = model as! MainModel
            a.eventPraise.v_on {
                print(a.name)

            }
        }
        
        
    }
    

}
class Main:NSObject{
    
    func loadData(ob:Observe?){
        
        
        var array = Array<Cat>()
        for i in 1...3{
            
            let m = MainModel()
            m.name = "wyy\(i)"
            array.append(m)
           
        }
        

//数组观察者,返回数组数组,table完成刷新
        ob?.v_array(true, v: { () -> Array<Cat>? in
            
            return array
        })
        
       
        
    }
   
    
}

Cell

  var ob = Observe()
    var clickOb = Observe()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        let label = CLabel()
        self.contentView.addSubview(label)
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 10)
        label.frame = CGRect.init(x: 20, y: 0, width:Screen.width()/2 - 20, height: 30)
        
        let button = CButton()
        button.setTitle("点赞", for: .normal)
        button.setTitleColor(UIColor.red, for: .normal)
        self.contentView.addSubview(button)
        button.frame = CGRect.init(x: Screen.width()/2, y: 0, width: Screen.width()/2 - 20, height: 30)

        label.v_text(ob: ob)
        button.v_on(ob: clickOb)
        
    }
override func setModel(_ amodel: Cat) {
        super.setModel(amodel)
        if amodel is MainModel{
            let a = amodel as! MainModel
            //更新数据
            ob.v_text { () -> String? in
                
                return a.name
            }
            clickOb.v_on {
                
                a.eventPraise.v_on?()
            }
            
            
            
        }
    }
    

相关文章

网友评论

      本文标题:致那些复杂的响应式框架5 全新移动端框架 CatFlow

      本文链接:https://www.haomeiwen.com/subject/podtmqtx.html