Giter Club home page Giter Club logo

dustturtle / mvvmframework-swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lovemo/mvvmframework-swift

0.0 2.0 0.0 966 KB

(Swift版)总结整理下一个快速开发框架,以更优雅的方式写代码,做一个代码艺术家。分离控制器中的代码,已加入cell自适应高度,自动缓存网络请求代码,降低代码耦合,提高开发效率。

License: MIT License

Ruby 0.10% Swift 16.75% Objective-C 82.03% Shell 0.91% C 0.22%

mvvmframework-swift's Introduction

MVVMFramework-Swift

###OC版本地址:https://github.com/lovemo/MVVMFramework ####本项目交流群:474292335 ####欢迎有兴趣的有好的想法的参与到项目中来

总结整理下一个快速开发MVVM框架(抛砖引玉),主要用于分离控制器中的代码,降低代码耦合程度,可以根据自己使用习惯调整代码。欢迎来喷,提issues。代码加入了cell自适应高度,自动缓存网络请求至sqlite数据库,更加高效的数据库存储库。

####usage: CocoaPods:

	platform :ios, '8.0'
	use_frameworks!
	pod 'SwiftMVVMKit'

##思维流程图示 image image ##现在的工程代码结构 image

模块构建


结构分析


##部分protocol示例

@objc public protocol SMKViewMangerProtocolDelegate: NSObjectProtocol {

     /**
     设置Controller的子视图的管理者为self
     
     - parameter superView: 一般指subView所在控制器的view
     
     - returns: return value description
     */
    optional func smk_viewMangerWithSuperView(superView: UIView)
    
     /**
     设置subView的管理者为self
     
     - parameter subView: 管理的subView
     
     - returns: return value description
     */
    optional func smk_viewMangerWithSubView(subView: UIView?)
    
     /**
     设置添加subView的事件
     
     - parameter subView: 管理的subView
     - parameter info:    附带信息,用于区分调用
     
     - returns: return value description
     */
    optional func smk_viewMangerWithHandleOfSubView(subView: UIView, info: String?)
    
     /**
     返回viewManger所管理的视图
     
     - returns: viewManger所管理的视图
     */
    optional func smk_viewMangerOfSubView() -> UIView
    
     /**
     得到其它viewManger所管理的subView,用于自己内部
     
     - parameter viewInfos: 其它的subViews
     
     - returns: return value description
     */
    optional func smk_viewMangerWithOtherSubViews(viewInfos: [NSObject : AnyObject]?)
    
     /**
     需要重新布局subView时,更改subView的frame或者约束
     
     - parameter updateBlock: 更新布局完成的block
     */
    optional func smk_viewMangerWithLayoutSubViews(updateBlock: (( ) -> ( ))?)
    
    /**
     使子视图更新到最新的布局约束或者frame
     */
    optional func smk_viewMangerWithUpdateLayoutSubViews()
}

代码示例

viewManger中代码实现

class FourthViewManger2: NSObject, SMKViewMangerProtocolDelegate {

    lazy var fourthView2 = FourthView2.loadInstanceFromNib()
    lazy var fourthView = UIView()
    
    func smk_viewMangerWithSuperView(superView: UIView) {
        superView.addSubview(fourthView2)
    }

    // 根据自身需要得到外界的视图view
    func smk_viewMangerWithOtherSubViews(viewInfos: [NSObject : AnyObject]?) {
        
        let view1 = viewInfos!["view1"] as! UIView
        fourthView = view1
        
        fourthView2.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(250, 250));
            make.top.equalTo(view1.snp_bottom).offset(20);
            make.left.equalTo(view1);
        }

    }
    
    // 根据外界view或model的变化重新布局自己所管理的字视图的位置
    func smk_viewMangerWithUpdateLayoutSubViews() {
        let  offset = CGFloat(arc4random_uniform(70) + 10)
        let  wh = CGFloat(arc4random_uniform(200) + 50)
        let  size = CGSizeMake(wh, wh)
        
        fourthView2.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.fourthView.snp_bottom).offset(offset);
            make.size.equalTo(size);
        }
        
        fourthView2.setNeedsLayout()
        UIView.animateWithDuration(0.5) { () -> Void in
            self.fourthView2.layoutIfNeeded()
        }
    }

}

UIViewController中部分代码实现

 /**
     tableView的一些初始化工作
     */
    func setupTableView() {

        table.separatorStyle = .None
        
        // 下拉刷新
       table.mj_header = MJRefreshNormalHeader { [weak self] () -> Void in
            if let strongSelf = self {
                strongSelf.viewModel.smk_viewModelWithGetDataSuccessHandler({ (array) -> () in
                    strongSelf.table.reloadData()
                })
                // 结束刷新
                self!.table.mj_header.endRefreshing()
            }
        }
        table.mj_header.automaticallyChangeAlpha = true
        
        table.tableHander = SMKBaseTableViewManger(cellIdentifiers: [MyCellIdentifier], didSelectBlock: { (_, _) -> Void in
            let vc = UIViewController.viewControllerWithStoryboardName("Main", vcIdentifier: "SecondVCID")
            self.navigationController?.pushViewController(vc, animated: true)
        })
        
        viewModel.smk_viewModelWithGetDataSuccessHandler { (array) -> () in
            self.table.tableHander .getItemsWithModelArray({ () -> [AnyObject] in
                    return array
                }, completion: { () -> () in
                    self.table.reloadData()
            })
        }
    }

demo效果

  • 只需实现加载请求以及配置自定义cell和上述代码,就能轻松实现以下效果,最重要的是代码解耦。

image

期待

  • 如果在使用过程中遇到BUG,希望你能Issues我,谢谢(或者尝试下载最新的代码看看BUG修复没有)
  • 如果在使用过程中发现功能不够用,希望你能Issues我,我非常想为这个框架增加更多好用的功能,谢谢

推荐

###应用架构文章 #####部分来自原创微信公众平台-移动开发前线

###MVVM学习文章

mvvmframework-swift's People

Contributors

lovemo avatar

Watchers

James Cloos avatar Zhenwei Guan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.