inkkit 4.0.1

inkkit 4.0.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2017年11月
SwiftSwift 版本4.0
SPM支持 SPM

Shaps 维护。




inkkit 4.0.1

Swift 支持

Swift 4.0

inkkit 默认支持 Swift 4.0,因此只需在你的 podfile 中包含 inkkit 即可使用。

pod 'InkKit'

Swift 3.2

要在 Swift 3.2/3.0 项目中使用 inkkit,确保指向 2.1.0 标签。

pod 'InkKit', '2.1.0'

Swift 2.3

为了在 Swift 2.3 项目中使用 inkkit,确保指向 swift2.3 分支。

pod 'InkKit', :git => 'https://github.com/shaps80/InkKit/', :branch => 'swift2.3'

Swift 2.2

如果你仍然需要 Swift 2.2,可以将版本指向如下: pod 'InkKit', '1.3.1'

示例代码

在这里看到的一切都是使用 inkkit 代码绘制的!实际上,除了一些 CGRect 实例外,这是绘制右侧图像所需的所有代码 ;)

绘制变得简单! inkkit 在行动中
让我们绘制右侧的屏幕。
Draw.fillRect(bgFrame, color: UIColor(hex: "1c3d64"))
let grid = Grid(colCount: 6, rowCount: 9, bounds: gridFrame)
let path = grid.path(includeComponents: [.Columns, .Rows])

Draw.strokePath(path, startColor: UIColor(white: 1, alpha: 0.15), endColor: UIColor(white: 1, alpha: 0.05), angleInDegrees: 90)

let rect = grid.boundsForRange(sourceColumn: 1, sourceRow: 1, destinationColumn: 4, destinationRow: 6)

drawCell(rect, title: "4x6", includeBorder: true, includeShadow: true)

Draw.addShadow(.Outer, path: UIBezierPath(rect: barFrame), color: UIColor(white: 0, alpha: 0.4), radius: 5, offset: CGSize(width: 0, height: 1))

Draw.fillRect(barFrame, color: UIColor(hex: "ff0083"))

let (_, navFrame) = barFrame.divide(20, fromEdge: .MinYEdge) "InkKit".drawAlignedTo(navFrame, attributes: [ NSForegroundColorAttributeName: Color.whiteColor(), NSFontAttributeName: UIFont(name: "Avenir-Book", size: 20)! ])

backIndicatorImage().drawAtPoint(CGPoint(x: 22, y: 30))

变更日志

v2.0.0

注意:由于这是一个 Swift 3 版本,我决定同时清理 API 并移除所有弃用警告。inkkit 2.0 应被视为新的 API。

  • Swift 3.0 支持
  • 更新 API 以支持 Swift 3.0 指南
  • 剪切变换
  • 透视变换
  • 度转换为弧度函数(及其逆函数)
  • 圆角,支持凹陷、凸起和直线
  • 新的颜色值类型

v1.3.1

  • macOS 支持
  • macOS 示例项目现已包含在内
  • 表格 重命名为 Grid
  • 表格 重命名为 GridComponents
  • 添加了用于处理路径的便利方法

v1.2.0

  • 阴影
  • 边框
  • 表格

v1.1.0

  • 图像
  • 字符串

v1.0

  • 填充
  • 描边
  • 几何形状

API

inkkit 提供许多有用的便利方法用于绘制和几何计算。它还可以跨平台工作,在 iOS & MacOS 上使用。

核心

如果以下便利方法不能解决你的需求,你可以直接使用添加到 CGContext 的新方法开始。

func draw(inRect:attributes:drawing:)

在用法上如下所示

CGContext.current?.draw(inRect: rect, drawing: { (context, rect, attributes) in
  Color.redColor.setFill()
  UIRectFill(rect)
})

这基本上封装了获取上下文、设置其框架和保存/恢复调用。如果你提供了额外的 DrawingAttributes 块,它还会为你预先配置上下文选项。

网格

init(colCount:rowCount:bounds:)
func positionForCell(atIndex:) -> (col: Int, row: Int)
func boundsForCell(atIndex:) -> CGRect
func boundsForRange(sourceColumn:sourceRow:destinationColumn:destinationRow:) -> CGRect
func boundsForCell(col:row:) -> CGRect
func enumerateCells(enumerator:(index:col:row:bounds:) -> Void)

使用网格可以非常方便地排列绘图,无需考虑位置、矩形转换等因素...

我通常只用于布局,但有时也能将其作为渲染内容使用(例如在提供的演示中)。

// components is a bitmask [ .Outline, .Rows, .Columns ]
func stroke(components:attributes:)

边框与阴影

支持.Outer.Inner.Center边框,以及.Outer.Inner阴影。

static func addBorder(type:path:attributes:)
static func addShadow(type:path:color:radius:offset:)

线条

static func strokeLine(startPoint:endPoint:startColor:endColor:angleInDegrees:attributes:)
static func strokeLine(startPoint:endPoint:color:attributes:)
static func strokePath(path:startColor:endColor:angleInDegrees:attributes:)

填充

static func fillPath(path:startColor:endColor:angleInDegrees:attributes:)

几何

许多绘图方法使用了下面的几何功能,但它们也可以用于您自己的项目。

func divide(atDelta:fromEdge:margin:) -> (slice, remainder)
func insetBy(edgeInsets:) -> CGRect
mutating func insetInPlace(edgeInsets:)
func alignedTo(rect:horizontal:vertical:) -> CGRect
func scaledTo(rect:scaleMode:) -> CGRect

func gradientPoints(forAngleInDegrees:) -> (start, end)
func scaledTo(size:scaleMode:) -> CGSize

func reversibleRect(fromPoint:toPoint:) -> CGRect

图像

还有一些针对图像的额外绘图方法。

func drawAlignedTo(rect:horizontal:vertical:blendMode:alpha:)
func drawScaledTo(rect:scaleMode:blendMode:alpha:)

static func circle(radius:attributes:) -> Image
static func draw(width:height:scale:attributes:drawing:) -> Image
static func draw(size:scale:attributes:drawing:) -> Image

字符串

最后,我们还提供了一些简单的字符串绘图方法。

func drawAlignedTo(rect:horizontal:vertical:attributes:constrainedSize:)
func sizeWithAttributes(attributes:constrainedSize:) -> CGSize
func drawAtPoint(point:attributes:)
func drawInRect(rect:withAttributes)

演示

要亲自尝试,请下载源代码并运行 Included 演示项目。

平台与版本

InkKit 支持以下平台

  • iOS 8.0 及更高版本
  • OSX 10.11 及更高版本

安装

InkKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'InkKit'

或者,您可以直接将文件拖放到 iOS 或 OSX 项目中。

作者

@shaps

许可

InkKit 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。