/SNDraw

Primary LanguageSwiftMIT LicenseMIT

SNDraw

SNDraw is a lightweigt library for iOS, which turns a series of touch events into a series of path elements, performing smoothing (to reduce the number of elements) as well as detecting sharp-turns.

struct SNPath

SNPath is an abstruct struct, which offers static functions to convert between a series of path elements (represented in an array of SNPathElement) and a CGPath or a SVG path.

static func path(from elements:[SNPathElement]) -> CGPath
static func elements(from path:CGPath) -> [SNPathElement]

These functions performs the conversion between a series of path elements and a CGPath

static func polyPath(from elements:[SNPathElement]) -> CGPath

This function converts a series of path elements into a CGPath, by turning curves into polylines by drawing lines between control points and anchor points.

static func svg(from elements:[SNPathElement]) -> String
static func elements(from svg:String) -> [SNPathElement]

These functions perform the conversion between a series of path elements and a SVG path (excluding "arc" commands).

protocol SNPathElement

SNPathElement is a protocol, which represents a path element. It has two methods to be implemented by the concrete structs.

func add(to path:CGMutablePath) -> CGMutablePath

This functions add the element to CGMutablePath.

func addAsPolygon(to path:CGMutablePath) -> CGMutablePath

This functions add the element to CGMutablePath, by turning curves into polylines by drawing lines between control points and anchor points.

public func svgString(_ prev:SNPathElement?) -> String

This function returns the SVG representation of the element. The prev specifies the previous path element in the sequence for optimization.

struct SNCloseSubpath, SNMove, SNLine, SNQuadCurve, SNBezierCurve

Those structs are concrete structs of SNPathElement protocol, representing close-subp, move, line, quadratic curve, and bezier curve element respectively.

struct SNPathBuilder

SNPathBuilder builds a series of path elements from a series of CGPoints, by performing smoothing (to reduce the number of elements) as well as detecting sharp-turns.

Typically, the user of this struct is a subclass of UIView or UIViewController, which receives a series of touch events.

public properties

    public var minSegment:CGFloat

It specifies the minimum segment length for smoothing. If smoothing is not necessary, specify 0.

    public var elements:[SNPathElement]

This is the series of path elements (each element conforms to SNPathSegment protocol), generated by calling start, move (multiple times) and end function.

public methods

init(minSegment:CGFloat)

The minSegment parameter of this constructor specifies the minimum segment length for smoothing.

public mutating func start(_ pt:CGPoint) -> CGPath

It indicates the beginning of touch events. The user of this struct typically calls this method when the user touches the view (from the touchesBegan method). It removes all the existing elements in the elements and adds a SNMove element to it.

public mutating func move(_ pt:CGPoint) -> CGPath?

It indicates of a touch move event. The user of this struct typically calls this method when the user moves a finger on the view (from the touchesMove method). It may or may not add a SNQuadCurve element (depending on the smoothing).

public mutating func end() -> CGPath

It indicates the end of touch events. The user of this struct typically calls this method when the user release the finger from the view (from the touchesEnded method). It adds a SNQuadCurve element.

class SNDrawView

SNDrawView is a subclass of UIView, which processes touch events and builds a path using SNPathBuilder.

public properties

public private(set) var builder = SNPathBuilder(minSegment: 25.0)

The client of SNDrawView can customize the smoothing behavior by changing the minSegment property of this property.

weak public var delegate:SNDrawViewDelegate?

The client of SNDrawView specify this delegate to receive didComplete callback.

public lazy var shapeLayer:CAShapeLayer

This is an instance of CAShapeLayer, which SNDrawView renders the path while processing the touch events. The client of SNDrawView may customize its properties, such as lineWidth, strokeColor, lineCap, lineJoin.

protocol SNDrawViewDelegate

func didComplete(_ elements:[SNPathElement]) -> Bool

SNDrawViewDelegate call this method when it receives touchesEnded message with the series of path elements.