JXUtils
该项目旨在减少在众多 iOS 应用中常见且奇怪的 boiler plate 代码,例如简单弹窗、从 Nib 实例化 UIView、设置渐变等。请随意提交问题或拉取请求。
功能
以下示例使用默认参数作为实现方式,但你可以在方法声明中查看每个方法的全参数列表。
UIDevice
- 更简单的方法来判断是否是 iPad/iPhone
- 更简单的方法来判断它是在横屏还是竖屏方向
示例
let isIPad = UIDevice.isPad()
let isIPhone = UIDevice.isPhone()
let isPortrait = UIDevice.isPortrait()
let isLandscape = UIDevice.isLandscape()
矢量操作
- 向任何符合 CSVector2Protocol 协议的类型(如 CGPoint 和 CGSize)添加各种矢量操作和有用的属性。
示例
struct CustomVector2: CSVector2Protocol {
var x: CSScalar
var y: CGScalar
static func constructFromVector(_ vector: CSVector2) -> Vector2 {
return Vector2.init(x: vector.x, y: vector.y)
}
}
let vector = CustomVector2(x:10,y:20)
print(vector * 2) // Output: CustomVector2(x:20,y:40)
print(vector / 2) // Output: CustomVector2(x:5,y:10)
print(vector / vector) // Output: CustomVector2(x:1,y:1)
print(vector * vector) // Output: CustomVector2(x:100,y:400)
print(-vector) // Output: CustomVector2(x:-10,y:-20)
//And various others...
序列
- 一些检查序列条件(无、任何、所有)的有用方法
- 计数
示例
let list = [1,2,3,4,5,6]
let isAllPositive = list.none({$0 < 0}) // Output: true
let hasNegative = list.any({$0 < 0}) // Output: false
let allLessThan10 = list.all({$0 < 10}) // Output: true
let evenNumbers = list.count({ $0 % 2 == 0 }) // Output: 3
UIViewController
- 从 Storyboard 实例化 ViewController 的简便方法
- 展示 Alert 的简便方法
示例
// Instantiate the LoginViewController from the Storyboard with name 'Login.storyboard', and with the identifier 'LoginViewController'
// If in the Storyboard there is no viewController with this identifier, the Initial viewController will be instantiated instead.
let viewController: LoginViewController = LoginViewController.fromStoryboard()
//Instantiate the UINavigationController from the initial viewController of the Storyboard with name 'Login.storyboard'
let navigationController: UINavigationController = LoginViewController.fromStoryboardWithNavigationController()
// Will present an UIAlertController with the inputed message, no title, no cancel button and with the button "Close".
UIViewController().showAlert(title: nil, message: "You did the thing with success!", actions: [CSUIViewControllerSettings.defaultAlertCloseAction()])
// Same as the the previous example.
UIViewController().showMessage(message:"You did the thing with success!")
// To modify some of the default behaviors, such as the Close and Cancel Actions (from showMessage) and the
// Storyboard naming pattern (used in fromStoryboard()), you can change the static closures of the CSUIViewControllerSettings structure.
NSObject
- 获取类名或实例类型或实例的简便方式。
示例
print(CustomType.className) // Output: "CustomType"
print(CustomType().className) // Output: "CustomType"
UIView
- 从Nib实例化UIView的更简单方式。
示例
// Instantiate the UIView of the type CustomView from the nib named 'CustomView'
let view = CustomView.fromNib()
CGRect + CGPoint + CGSize
- 更简单的构造函数。
- 通过声明式样式轻松更改属性。
- 向CGPoint和CGSize添加了多个数学运算(CGRect将在不久的将来接收它们)。
示例
let frame = CGRect.zero.with(x:100, y:10, width: 10).adding(onX:50, onHeight:100) //Output: CGRect(x:150, y:10, width:10, height:100)
let frame2 = frame.with(origin: frame.origin / 2) //Output: CGRect(x:75, y:5, width:10, height:100)
let frame3 = frame.with(size: frame.size * 2) //Output: CGRect(x:150, y:10, width:20, height:200)
CALayer
- 插入渐变的更简单方式。
示例
self.layer.insertGradient(from: .bottom, to: .top, with: [UIColor.black, UIColor.white], distribution: .fibonacci) //Returns the CAGradientLayer after insertion, but the result is discardable.
示例
要运行示例项目,请首先克隆存储库,然后从示例目录运行pod install
。
安装
CupertinoStandards 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 文件中。
pod 'CupertinoStandards'
作者
MrJorgeXavier
许可证
CupertinoStandards 使用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。