zlq4863947

有妖气(仿)iOS应用源码解读(一)

序言

最近吹牛说要做iOS漫画软件,所以在github上找到下面源代码:

解读源码地址:https://github.com/spicyShrimp/U17
实际画面:

启动图标 启动后显示画面

开发環境

  • Xcode 9.2
  • Swift 4.0.3

启动应用

作者应该是没有做splash,所以打开后会短暂白屏,

  • AppDelegate.swift
    AppDelegate为app启动的代理类,也就是程序的入口。
    每个ios应用程序都有一个AppDelegate.swift。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    //
    // AppDelegate.swift
    // U17
    //
    // Created by charles on 2017/9/29.
    // Copyright © 2017年 None. All rights reserved.
    //

    // UI工具包
    import UIKit
    // @see https://blog.csdn.net/xoxo_x/article/details/76390775
    // 网络请求的开源库
    import Alamofire
    // 键盘监听
    import IQKeyboardManagerSwift

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    // 延时加载reachability
    lazy var reachability: NetworkReachabilityManager? = {
    // 实时监测与http://app.u17.com连接状况
    return NetworkReachabilityManager(host: "http://app.u17.com")
    }()

    // 设置为竖屏显示
    var orientation: UIInterfaceOrientationMask = .portrait

    // 应用加载完成时
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    configBase()

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.backgroundColor = UIColor.white
    window?.rootViewController = UTabBarController()
    window?.makeKeyAndVisible()
    //MARK: 修正齐刘海
    // UHairPowder.instance.spread()

    return true
    }

    // 基本配置
    func configBase() {
    // 开启键盘监听
    IQKeyboardManager.sharedManager().enable = true
    // 点击背景收起键盘
    IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true

    // 本地存储对象
    let defaults = UserDefaults.standard
    if defaults.value(forKey: String.sexTypeKey) == nil {
    // 设置性别类型为1(1:男性,2:女性?)
    defaults.set(1, forKey: String.sexTypeKey)
    // 保存数据
    defaults.synchronize()
    }

    reachability?.listener = { status in
    switch status {
    // 使用蜂窝数据时
    case .reachable(.wwan):
    // 通知显示
    UNoticeBar(config: UNoticeBarConfig(title: "主人,检测到您正在使用移动数据")).show(duration: 2)
    default: break
    }
    }
    // 开始监听
    reachability?.startListening()
    }

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return orientation
    }


    }

    extension UIApplication {
    class func changeOrientationTo(landscapeRight: Bool) {
    guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
    // 横屏右转时
    if landscapeRight == true {

    // 代理的物理方向设置为右横屏
    delegate.orientation = .landscapeRight
    // 为window设置支持的方向
    UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
    // 设备设置为右横屏
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
    } else {
    delegate.orientation = .portrait
    UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
    }
    }
    }
🐶 您的支持将鼓励我继续创作 🐶