chengfengjie / chengfengjie.github.io

我叫MT
1 stars 0 forks source link

th-ios项目笔记 #7

Open chengfengjie opened 6 years ago

chengfengjie commented 6 years ago

用cocoapods管理第三方库,Podfile文件如下:

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
inhibit_all_warnings!

use_frameworks!

def share_pods
  pod 'SnapKit', '~> 4.0.0'
  pod 'SVProgressHUD', '~> 2.1.2'
  pod 'UITextView+Placeholder', '~> 1.2.0'
  pod 'Mantle', '~> 2.1.0'
  pod 'UIColor+Additions', '~> 2.0.2'
  pod 'MBProgressHUD', '~> 1.0.0'
  pod 'DateTools', '~> 2.0.0'
  pod 'CBStoreHouseRefreshControl', '~> 1.0'
  pod 'YYWebImage', '~> 1.0.5'
  pod 'VTMagic', '~> 1.2.4'
  pod 'Texture', '>= 2.0'
  pod 'Then'
  pod 'SwiftyJSON', '~> 3.1.4'
  pod 'ReactiveCocoa', '~> 7.0'
  pod 'JYCarousel', '~> 0.0.3'
  pod 'SwiftyAttributes', '~> 4.1.0'
  pod 'RZTransitions', '~> 1.2.1'
  pod 'Alamofire', '~> 4.6.0'
end

target 'th-ios' do
  share_pods
end
target 'th-demo' do
    pod 'WeexSDK', '~> 0.17.0'
    share_pods
end
target 'th-ios-pro' do
    share_pods
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = ‘4.0’
    end
  end
end

其中有三个target: 解释如下 th-ios: 此target用来做开发 th-demo: 用来做项目的一些测试 th-ios-pro: 此target用来打包发布

打包脚本: https://github.com/chengfengjie/chengfengjie.github.io/issues/5

chengfengjie commented 6 years ago

项目基本思路:

UI层由三部分组成: viewController: 负责数据绑定(viewModel到viewLayout的数据的绑定交互逻辑)和处理控制器跳转 viewLayout: 负责界面的布局,是一个protocol, 控制器只需要遵循此协议实现协议方法即可 viewModel: 负责数据层

网络层基本逻辑 ThApi:此protocol实现了基本的网络请求方法,返回一个ReactiveCocoaSignal<JSON, RequestError> 其他的每一个模块的特定接口是一个协议,继承 ThApi 实现特定的Api, 例如: HomeApi的实现

protocol HomeApi: ThApi {}

extension HomeApi {

    /// 获取首页分类
    ///
    /// - Parameters:
    ///   - isCity: 是否特定城市
    ///   - cityName: 城市名称
    func requestCate(isCity: Bool = false, cityName: String = "") -> Signal<JSON, RequestError> {
        var params: [String: Any] = [:]
        if isCity {
            params["isCity"] = "1"
            params["cityName"] = cityName
        }
        return self.request(method: ThMethod.getCate, data: params)
    }

    /// 获取首页文章列表
    ///
    /// - Parameters:
    ///   - cateId: 分类ID
    ///   - pageNum: 页码
    /// - Returns: result
    func requestCateArticleData(cateId: String, pageNum: Int) -> Signal<JSON, RequestError> {
        let params: [String: Any] = [
            "catid": cateId,
            "page": pageNum.description
        ]
        return self.request(method: ThMethod.getHomeCateList, data: params)
    }
}

viewMode只需要遵循此协议,就可以使用这些方法调用接口

chengfengjie commented 6 years ago

SwiftyAttributes库的作用: 使用 Texture 作为UI布局的框架,存在的问题就是所有的 文本显示控件例如:ASTextNode,ASButtonNode的标题等等都需要设置的是一个NSAttributeString而不是String,系统库构建NSAttributeString写法又比较蛋疼,所以引入 SwiftyAttributes, 构建 NSAttributeString变得异常简单,代码如下:

  self.titleTextNode.attributedText = dataJSON["title"].stringValue
            .withFont(Font.thin(size: 18))
            .withTextColor(Color.color3)
            .withParagraphStyle(ParaStyle.create(lineSpacing: 5, alignment: .justified))

其中 dataJSON 是一个SwiftyJSON结构体