AppleBezierPath - UBogun/Xojo-iosLib GitHub Wiki

Inherits from AppleObject
Memory leak check: creating millions of Paths raised the memory consumption, but only up to small limit.
Status: complete

This is an implementation of UIBezierPath, the iOS native class for vector shapes. It is exactly the same class that is used for Xojo’s iOSPath class, only that AppleBezierPath has all of the class feature's available. It would have been nicce to add the features as additions via a module only, but I haven't found a way to retrieve the handle of an iOSPath object yet. So, sadly, both are not interchangeable, although they can be used together for graphics purposes.

General information

Compared to iOSPath you will find a few more construction features, more fine-tunings and the ability to draw directly into an AppleCGContext object, plus the conversion to and from AppleCGPaths. Sadly, because of the limitations stated above, there is no conversion between iOSPath and AppleBezierPath at this moment.
Please keep in mind that one object of AppleBezierPath can contain an arbitrary number of subpaths.

Like many UIKit graphical classes, AppleBezierPath has features to work directly on the current context. So inside the draw event of an iOSLibCanvas this is perfectly working code (taken from the flatness image code below):

AppleColor.RedColor.SetStroke
Dim p as new AppleBezierPath
p.MoveToPoint FoundationFrameWork.NSMakePoint (0,0)
p.AddCurve FoundationFrameWork.NSMakePoint (me.Width, me.Height), FoundationFrameWork.NSMakePoint (me.Width /3, 0), FoundationFrameWork.NSMakePoint (me.Width /1.5, me.Height)
p.Flatness = 0
p.Stroke

Constructors

Constructor (): Creates and returns a new BezierPath object.

Constructor (ARect as FoundationFramework.NSRect): Creates and returns a new BezierPath object initialized with a rectangular path.

More constructors are available as Shared methods.

Properties

Bounds As FoundationFramework.NSRect (read-only): The bounding rectangle of the path.

CGPath As AppleCGPath: The Core Graphics representation of the path. Can also be used to define the AppleBezierpath via a AppleCGPath.

CurrentPoint As FoundationFramework.NSPoint (read-only): The current point in the graphics path.

Empty As Boolean (read-only): True if the path object contains no elements.

Flatness As Double: The factor that determines the rendering accuracy for curved path segments.
Flatness
A curved path with a flatness of 0 (in red) and the same path with a flatness of 500 (in black)

LineCapStyle As CoreGraphicsFramework.CGLineCap: The shape of the path’s end points when stroked. Possible values are butt, round and square.

LineJoinStyle As CoreGraphicsFramework.CGLineJoin: The shape of the joints between connected segments of a stroked path. Possible values are miter, round and bevel. For a selection of miter, MiterLimit is respected.

LineWidth As Double: The line width of the path.

MiterLimit As Double: The limiting value that helps avoid spikes at junctions between connected line segments with a LineJoinStyle of Miter.
If the ratio of the miter length—that is, the diagonal length of the miter join—to the line thickness exceeds the miter limit, the joint is converted to a bevel join. The default miter limit is 10, which results in the conversion of miters whose angle at the joint is less than 11 degrees.

UsesEvenOddFill As Boolean: A Boolean indicating whether the even-odd winding rule for determining if a point inside a path’s bounds is filled or not is in use for drawing paths. Default is False.

Methods

For a better picture of the path method’s parameters, refer to Apple’s UIBezierPath documentation
Please note that a new empty AppleBezierPath has no CurrentPoint set. If you want to start constructing it with one of the addxx methods, you have to call MoveToPoint first.

AddArc (Center as FoundationFrameWork.NSPoint, radius as Double, startAngle as Double, endAngle As Double, optional clockwise as Boolean): Appends an arc to the path.

AddClip(): Intersects the area enclosed by the path with the clipping path of the current graphics context and makes the resulting shape the current clipping path.

AddCurve (EndPoint as FoundationFrameWork.NSPoint, ControlPoint1 as FoundationFrameWork.NSPoint, ControlPoint2 as FoundationFrameWork.NSPoint): Appends a cubic Bézier curve to the path.

AddLine (Point as FoundationFrameWork.NSPoint): Appends a straight line to the path, beginning from CurrentPoint.

AddQuadCurve (EndPoint as FoundationFrameWork.NSPoint, ControlPoint as FoundationFrameWork.NSPoint): Appends a quadratic Bézier curve to the path.

AppendPath (Path As AppleBezierPath): Appends the contents of another path object to the path.

ApplyTransform (Transform as CGAffineTransform): Transforms all points in the path using the specified affine transform matrix.

Close(): Closes the most recently added subpath.

ContainsPoint (Point As FoundationFramework.NSPoint) As Boolean: Whether the area enclosed by the path contains the specified point.

Fill(): Paints the region enclosed by the path using the current drawing properties. Useful for CGContext drawing.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

FillWithBlendmode (blendmode as CoreGraphicsFramework.CGBlendMode, alpha as double): Paints the region enclosed by the path using the specified blend mode and transparency values. Blend mode offers you color blending features that even exceed those of Photoshop. Useful for CGContext drawing.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

GetLineDash (byref count as integer, byref phase as double) As Double(): Retrieves the line-stroking pattern for the path. The returned Array of Doubles is the lengths of lines and gaps in your pattern. Count tells you on return the number of Double values, in other words, Doubles().ubound +1. Phase Is the beginning point of the pattern. For a value of 4 in a pattern of 2 - 3 - 3 - 4 it would be the middle of the first gap.

MoveToPoint (Point As FoundationFrameworkNSPoint): Moves the path’s current point to the specified location. Ends the current subpath (if there is one) without closing it and sets the potential beginning point for a new one.

RemoveAllPoints(): Removes all points from the path, effectively deleting all subpaths.

ReversePath() As AppleBezierPath: Creates and returns a new bezier path object with the reversed contents of the current path.

SetLineDash(Pattern() As Double, count as integer = 0, phase as double = 0): Sets the line-stroking pattern for the path. See GetLineDash for parameter information. A value of count below Pattern.ubound + 1 would restrict the pattern to the appropriate number of lines and gaps.

Stroke(): Draws a line along the path using the current drawing properties. Useful inside CGContext.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

StrokeWithBlendMode(blendmode as CoreGraphicsFramework.CGBlendMode, alpha as double = 1): Draws a line along the path using the specified blend mode and transparency values. Useful inside CGContext.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

Shared Methods

FromCGPath (Path As AppleCGPath) As AppleBezierPath: Creates and returns a new UIBezierPath object initialized with the contents of a Core Graphics path.

PathWithArc (ArcCenter as FoundationFrameWork.NSPoint, radius as Double, startAngle as Double, endAngle as Double, optional clockwise as boolean) As AppleBezierPath: Creates and returns a new UIBezierPath object initialized with an arc of a circle.

PathWithOval (aRect as FoundationFrameWork.NSRect) As AppleBezierPath: Creates and returns a new BezierPath object initialized with an oval path inscribed in the specified rectangle.

PathWithRect (aRect as FoundationFrameWork.NSRect) As AppleBezierPath: The same as Constructor (aRect).

PathWithRoundedRect (aRect as FoundationFrameWork.NSRect, CornerRadius As Double) As AppleBezierPath: Creates and returns a new BezierPath object initialized with a rounded rectangular path.

PathWithRoundedRect (aRect as FoundationFrameWork.NSRect, CornerRadii As FoundationFramework.NSSize, topLeft as boolean, topRight as Boolean, bottomLeft as Boolean, BottomRight as Boolean) As AppleBezierPath: Creates and returns a new UIBezierPath object initialized with a rectangular path where the specified corners are rounded.