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 HandyJSON>(_ type: T.Type) -> Observable<T> {

        return flatMap { response -> Observable<T> in

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

        }

    }

}


extension Response {

    func mapModelHandyJSON>(_ 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'


你可能感兴趣的:(RxSwift,Kingfisher,Moya,HandyJSON的结合:APP启动时增加了网络上下载的图片)