iOS-Utils 1.6.2

iOS-Utils 1.6.2

Robert Koch 维护。



iOS-Utils 1.6.2

  • 作者
  • ark-develop

iOS-Utils

Version Swift 5 Platforms Carthage compatible License

iOS-Utils 的目的是汇集一些有用的实用类、扩展和协议,以使 iOS 开发变得更加容易。

要求

  • iOS 11.0+
  • Xcode 10.2+
  • Swift 5.0+

安装

CocoaPods

CocoaPods 是 Swift 和 Objective-C Cocoa 项目的依赖管理器。

要使用 CocoaPods 将 iOS-Utils 集成到您的 Xcode 项目中,请将以下内容添加到 Podfile:

pod 'iOS-Utils', '~> 1.6'

因为集成整个pod可能过于复杂,您可以另选指定您想要包含的子规范。

子规范 包含的实用工具
可设计的 DesignableButton, DesignableView
AlertController UIAlertController+Convenience, UIAlertController+UIWindow
键盘 键盘通知,键盘观察者,键盘响应者

要在您的代码中包含一个或多个特定的子模块,请在Podfile中指定以下依赖项,为每个所需的子规范分别指定

pod 'iOS-Utils/{Sub Spec}', '~> 1.6'

例如,要包含您项目中Designable类,请按以下步骤添加到您的Podfile中

pod 'iOS-Utils/Designable', '~> 1.6'

Carthage

Carthage 是 Cocoa 应用程序的依赖关系管理器,它提供二进制框架并允许完全控制项目结构和设置。

要在 Xcode 项目中使用 Carthage 集成 iOS-Utils,请创建一个 Cartfile 并添加以下内容

github "ark-develop/iOS-Utils" ~> 1.6

Swift Package Manager

Swift Package Manager (Swift Package Manager) 是管理 Swift 代码分布的工具。

要使用 Swift Package Manager 将 iOS-Utils 集成到您的 Xcode 项目中,请将其作为依赖项添加

dependencies: [
    .package(url: "https://github.com/ark-develop/iOS-Utils.git", .upToNextMajor(from: "1.6.2"))
]

实用工具

DesignableView & DesignableButton

DesignableView (UIControl) 和 DesignableButton (UIButton) 是 IBDesignable 视图,允许在 Interface Builder 中操作属性,例如圆角或阴影,并在制作过程中显示这些更改。

可检查属性

  • cornerRadius
  • roundTopLeft
  • roundTopRight
  • roundBottomLeft
  • roundBottomRight
  • borderColor
  • borderWidth
  • shadowColor
  • shadowOffset
  • shadowOpacity
  • shadowRadius
  • gradientStartColor
  • gradientStartPoint
  • gradientEndColor
  • gradientEndPoint
  • highlightAlpha (仅适用于 DesignableView)

ClassName

ClassName 是在 NSObject 上进行简单扩展,为任何 NSObject 添加了一个 className 属性,作为静态变量和实例变量。

let className = YourClass.className // "YourClass"
let classInstance = YourClass()
let className = classInstance.className // "YourClass"

UIAlertController+Convenience

UIAlertController+Convenience 是对 UIAlertController 的扩展,使得创建 UIAlertView 变得更加简单,同时也提高了代码的可读性。

标准 UIAlertController 创建

let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert)

let okAction = UIAlertAction(title: "Okay",
                             style: .default,
                             handler: { _ in
                                // Okay pressed
                             })

let cancelAction = UIAlertController(title: "Cancel",
                                     style: .cancel,
                                     handler: { _ in
                                        // Cancel pressed
                                     })

alert.addAction(okAction)
alert.addAction(cancelAction)

简化的 UIAlertController 创建

let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert,
                              alertActions: [
                                  .okay({ _ in
                                      // Okay Pressed
                                  }),
                                  .cancel({ _ in
                                      // Cancel Pressed
                                  })
                              ])

let okAction: AlertAction = .okay({ _ in
    // Okay pressed
})

let cancelAction: AlertAction = .cancel({ _ in
    // Cancel pressed
})

let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert,
                              alertActions: [
                                  okAction,
                                  cancelAction
                              ])

UIAlertController+UIWindow

UIAlertController+UIWindow 是对 UIAlertController 的扩展,使得 Alerts 呈现得类似于 UIAlertView 的工作方式。使用此实现,可以在任何地方显示 Alerts,无需将 UIViewController(或其子类)作为参数。调用时会创建一个 UIWindow,并将 Alert 在该窗口中显示。当 Alert 被关闭时,应用的 UIWindow 将重新设置为键窗体。

UIAlertController+UIWindow 与具有首选样式 .alert 和 .actionSheet 的 UIAlertController 兼容。

// Convenience initializer from UIAlertController+Convenience.swift
let alert = UIAlertController(title: "title",
                              message: "message",
                              preferredStyle: .alert,
                              alertActions: [
                                  .okay({ _ in
                                      // Okay Pressed
                                  }),
                                  .cancel({ _ in
                                      // Cancel Pressed
                                  })
                              ])

alert.show() // Shows the alert

键盘通知信息

KeyboardNotificationInfo 是一个用于在响应键盘事件时发送的通知对象的数据模型。

通知属性

  • animationDuration (时间间隔)
  • animationOptions (UIView AnimationOptions?)
  • beginFrame (CGRect)
  • endFrame (CGRect)
  • isLocalUser (布尔值)

KeyboardObservable

KeyboardObservable 是一个协议,它增加了用于注册/注销键盘通知的功能。此外,它公开了在事件触发时调用的函数。

class FooViewController: UIViewController, KeyboardObservable {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        registerForKeyboardEvents()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unregisterForKeyboardEvents()
    }

    func keyboardWillShow(_ notification: Notification) {
        // notification is the Notification object containing the keyboard information
    }

    func keyboardWillHide(_ notification: Notification) {
        // notification is the Notification object containing the keyboard information
    }
}

注意: 目前仅注册了 UIResponder.keyboardWillShowNotificationUIResponder.keyboardWillHideNotification 通知。

KeyboardRespondable

KeyboardRespondable 在 KeyboardObservable 的基础上稍作扩展,添加了自动处理视图内容的内边距。

该协议需要 respondableViews 数组,它是一个 ContentInsetAdjustable 视图数组,其唯一要求是实现者必须有一个可变的 contentInset 属性。

KeyboardRespondable 会根据键盘和 respondableViews 之间的重叠量自动调整 respondableViews 的内容内边距,当键盘显示和隐藏时。

class FooViewController: UIViewController, KeyboardRespondable {
    @IBOutlet private var scrollViewOne: UIScrollView!
    @IBOutlet private var scrollViewTwo: UIScrollView!

    var respondableViews: [ContentInsetAdjustable] {
        return [
            scrollViewOne,
            scrollViewTwo
        ]
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        registerForKeyboardEvents()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        unregisterForKeyboardEvents()
    }
}

注意: 如果在代码实现中添加了 keyboardWillShow(_:) 和/或 keyboardWillHide(_:) 函数,则它们将覆盖默认实现,并且滚动视图将无法自动正确调整其内边距。