RxSwift,Kingfisher,Moya,HandyJSON的结合:APP启动时增加了网络上下载的图片

在主要的MainViewController.swift中:

import UIKit

import Kingfisher

import Moya

import RxSwift


class MainViewController: UITabBarController {


    let provider = RxMoyaProvider<ApiManager>()

    let launchView = UIImageView()

    let dispose = DisposeBag()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setLaunchView()

    }


    func setLaunchView() {

        

        launchView.frame = CGRect.init(x: 0, y: 0, width: screenW, height: screenH)

        launchView.alpha = 0.99

        launchView.backgroundColor = UIColor.black

        view.addSubview(launchView)

        

        provider

            .request(.getLaunchImg)

            .mapModel(LaunchModel.self)

            .subscribe(onNext: { (model) in

                if let imgModel = model.creatives?.first {

                    self.launchView.kf.setImage(with: URL.init(string: imgModel.url!), placeholder: nil, options: nil, progressBlock: nil, completionHandler: { (_, _, _, _) in

                        UIView.animate(withDuration: 1.5, animations: {

                            self.launchView.alpha = 1

                        }) { (_) in

                            UIView.animate(withDuration: 0.3, animations: {

                                self.launchView.alpha = 0

                            }, completion: { (_) in

                                self.launchView.removeFromSuperview()

                            })

                        }

                    })

                }

            })

            .addDisposableTo(dispose)

        

    }

    

}



在ApiManager.swift中:


import Foundation

import Moya


enum ApiManager {

    case getLaunchImg

    case getNewsList

    case getMoreNews(String)

    case getThemeList

    case getThemeDesc(Int)

    case getNewsDesc(Int)

}


extension ApiManager: TargetType {

    /// The target's base `URL`.

    var baseURL: URL {

        return URL.init(string: "http://news-at.zhihu.com/api/")!

    }

    

    /// The path to be appended to `baseURL` to form the full `URL`.

    var path: String {

        switch self {

        case .getLaunchImg:

            return "7/prefetch-launch-images/750*1142"

        case .getNewsList:

            return "4/news/latest"

        case .getMoreNews(let date):

            return "4/news/before/" + date

        case .getThemeList:

            return "4/themes"

        case .getThemeDesc(let id):

            return "4/theme/\(id)"

        case .getNewsDesc(let id):

            return "4/news/\(id)"

        }

    }

    

    /// The HTTP method used in the request.

    var method: Moya.Method {

        return .get

    }

    

    /// The parameters to be incoded in the request.

    var parameters: [String: Any]? {

        return nil

    }

    

    /// The method used for parameter encoding.

    var parameterEncoding: ParameterEncoding {

        return URLEncoding.default

    }

    

    /// Provides stub data for use in testing.

    var sampleData: Data {

        return "".data(using: String.Encoding.utf8)!

    }

    

    /// The type of HTTP task to be performed.

    var task: Task {

        return .request

    }

    

    /// Whether or not to perform Alamofire validation. Defaults to `false`.

    var validate: Bool {

        return false

    }

}



在LaunchModel.swift中



import Foundation

import HandyJSON


struct LaunchModel: HandyJSON {

    var creatives: [LaunchModelImg]?

}


struct LaunchModelImg: HandyJSON {

    var url: String?

    var text: String?

    var start_time : Int?

    var impression_tracks: [String]?

}



在ToModelExtension.swift中:


import Foundation

import RxSwift

import Moya

import HandyJSON


extension ObservableType where E == Response {

    public func mapModel<T: HandyJSON>(_ type: T.Type) -> Observable<T> {

        return flatMap { response -> Observable<T> in

            return Observable.just(response.mapModel(T.self))

        }

    }

}


extension Response {

    func mapModel<T: HandyJSON>(_ type: T.Type) -> T {

        let jsonString = String.init(data: data, encoding: .utf8)

        return JSONDeserializer<T>.deserializeFrom(json: jsonString)!

    }

}




pod 'Moya', '~> 8.0.0-beta.5'

pod 'Moya/RxSwift'

pod 'Kingfisher'

pod 'HandyJSON', '~> 1.5.2'

pod 'RxSwift',    '~> 3.0'

pod 'RxCocoa',    '~> 3.0'

pod 'RxDataSources', '~> 1.0'

pod 'Then', '~> 2.1.0'

pod 'SwiftDate', '~> 4.0.11'


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值