First, Introduction
The Coregraphics core graphics frame is more biased to the bottom than the Uikit frame. In the Objective-c project, Coregraphics methods are written in C language style, and they do not support the automatic reference counting of objective-c, and when programming with this framework, the developer should manage the memory manually. In the Swift project, Apple used the swift language to reconstruct the Coregraphics mine, redefining Cgpath,cgmutablepaht and so on for the class. Cgpath can be understood as the path of the graph, in the OBJECTIVE-C project, in fact, the system definition of an internal structure, developers can not directly use, developer Cgpathref and Cgmutablepathref alias as a reference to Cgpath, in fact, Cgpathref and Cgmutablepathref are all pointers to cgpath struct types, but one is a const type that cannot be modified, one can be modified, and the system is defined as follows:
typedef struct CGPATH *cgmutablepathref;
typedef const struct CGPATH *cgpathref;
Second, Cgpath creation and memory management of related methods
The related methods of Cgpath creation and memory management are listed as follows:
This method gets the unique identity of the Cgpath class in the Coregraphics framework
Cftypeid is actually an alias for an unsigned integer that defines an identity Cgpath to 280 for each class in the Coregraphics framework
Cftypeid Cgpathgettypeid (void);
This method creates a srtuct Cgpath * pointer that can be understood as a variable Cgpath class
Cgmutablepathref cgpathcreatemutable (void);
This method creates a cgpathref with a cgpathref.
Cgpathref cgpathcreatecopy (cgpathref path);
This method returns after the path is transform transformed when the cgpathref is created through Cgpathref.
Cgpathref Cgpathcreatecopybytransformingpath (cgpathref path, const cgaffinetransform * transform);
This method creates a variable cgmutablepathref through CGPATHREF.
Cgmutablepathref cgpathcreatemutablecopy (cgpathref path);
As above, a transform transformation is made on the basis of the cgmutablepathref created in return
Cgmutablepathref Cgpathcreatemutablecopybytransformingpath (cgpathref path, const cgaffinetransform * transform)
This method creates a rectangular path with the first parameter for the rectangle to draw the 2nd parameter is the transform transform to be made
Cgpathref cgpathcreatewithrect (cgrect rect,const cgaffinetransform * transform);
This method creates an elliptical path
Cgpathref cgpathcreatewithellipseinrect (cgrect rect, const cgaffinetransform * transform);
This method is used to create rounded rectangular paths
/*
Rect: A rectangular area drawn
Cornerwidth: Transverse fillet dimensions
Cornerheight: Longitudinal fillet dimensions
*/
Cgpathref cgpathcreatewithroundedrect (cgrect rect, cgfloat cornerwidth, cgfloat cornerheight,const CGAffineTransform * Transform);
This method is used to create a dashed path
/*
This method is slightly more complex with the following parameter meanings:
Path: route to be dashed
Phase: Draws a dashed line from the first part of the lengths array
LENGTHS:C style array where the CGFloat value indicates the drawing length of each dashed line, for example, if the incoming array is {10,5}, the dashed lines are drawn with a length of 10, and a blank with a length of 5 is looping
Count: This parameter needs to be set to the length of the lengths array
*/
Cgpathref Cgpathcreatecopybydashingpath (cgpathref path, const cgaffinetransform * transform,cgfloat phase, const CGFLOA T *lengths, size_t count);
To create a slash through Cgpathref
/*
LineWidth: Setting the line width
LineCap: Set the line cap style optional parameters as follows:
typedef cf_enum (int32_t, Cglinecap) {
Kcglinecapbutt, the endpoint of the default style line is precisely to the point
Kcglinecapround, the end of a sleek end line is an arc with a radius of half the line width
Kcglinecapsquare a sharp transition
};
LineJoin: Set the style of the connection line optional parameters are as follows:
typedef cf_enum (int32_t, Cglinejoin) {
Kcglinejoinmiter,//with sharp corners as the turning point of the connecting line
Kcglinejoinround,//turning with rounded corners as a connecting line
Kcglinejoinbevel//Bézier-style twist
};
Miterlimit: This value determines the degree of sharpness of the angle at which the line is connected
*/
Cgpathref Cgpathcreatecopybystrokingpath (cgpathref cg_nullable path, const cgaffinetransform * __nullable Transform, CGFloat linewidth, Cglinecap linecap,cglinejoin lineJoin, CGFloat miterlimit);
Manually make Cgpathref reference count +1
Cgpathref cgpathretain (cgpathref cg_nullable path);
Manually make Cgpathref reference count-1
void Cgpathrelease (cgpathref cg_nullable path);
Customize a view view to draw the interface in its DrawRect method, the sample code is as follows:
-(void) DrawRect: (cgrect) Rect {
Gets the current drawing context
Cgcontextref contextref = Uigraphicsgetcurrentcontext ();
Cgpoint Center = cgpointmake (RECT.SIZE.WIDTH/2, RECT.SIZE.HEIGHT/2);
Create Rounded Rectangle path
Cgpathref pathref = Cgpathcreatewithroundedrect (CGRectMake (center.x-50, CENTER.Y-50, N, +), (+), nil);
Dashed a path
CGFloat floats[] = {10,5};
Pathref = Cgpathcreatecopybydashingpath (pathref, nil, 0, floats, 2);
Set Drawing color
[[Uicolor Redcolor] setstroke];
Add a path to the drawing context
Cgcontextaddpath (Contextref, pathref);
To draw
Cgcontextdrawpath (Contextref, Kcgpathstroke);
Memory release
Cgpathrelease (PATHREF);
Cgcontextrelease (CONTEXTREF);
}
The post-run effect is as follows:
Three, Cgpath method of path drawing
Move the path to a point as a starting point
void Cgpathmovetopoint (Cgmutablepathref path,const cgaffinetransform * m, cgfloat x, cgfloat y);
Move the path to a point to draw a line
void Cgpathaddlinetopoint (Cgmutablepathref path,const cgaffinetransform * m, cgfloat x, cgfloat y);
Add a two-time Bezier curve to a path
/*
CPX: The x-coordinate of the control point
CPY: The y-coordinate of the control point
*/
void Cgpathaddquadcurvetopoint (Cgmutablepathref path,const cgaffinetransform * m, cgfloat CPX, CGFloat cpy,CGFloat x, CGF Loat y);
Add a three-time Bezier curve
void Cgpathaddcurvetopoint (Cgmutablepathref path,const cgaffinetransform * m, cgfloat cp1x, CGFloat cp1y,CGFloat cp2x, CG Float cp2y, CGFloat x, cgfloat y);
This method is used to close the path after calling this method and the end point of the path is closed
void Cgpathclosesubpath (cgmutablepathref path);
Append a rectangle to a path
void Cgpathaddrect (cgmutablepathref path, const cgaffinetransform * m, cgrect rect);
Appends a set of rectangles to a path
void Cgpathaddrects (cgmutablepathref path, const cgaffinetransform * m, const cgrect * rects,size_t count);
Append a set of lines to a path
void Cgpathaddlines (cgmutablepathref path, const cgaffinetransform * m, const cgpoint * __nullable points, size_t count);
Add a set of ellipses
void Cgpathaddellipseinrect (cgmutablepathref cg_nullable path,const cgaffinetransform * m, CGRect rect);
Append a set of arcs to a path
/*
X: center X coordinate
Y: Center y coordinate
Radius: Arc radius
StartAngle: Starting angle
Endangle: Terminating angle
Clockwise: whether to draw clockwise
*/
void Cgpathaddarc (cgmutablepathref path, const cgaffinetransform * m, cgfloat x, cgfloat y, cgfloat radius, cgfloat start Angle, CGFloat Endangle, bool clockwise);
Append a set of arcs to a path
/*
X: center X coordinate
Y: Center y coordinate
Radius: Arc radius
StartAngle: Starting angle
Delta: Arc drawn in radians 2π for the entire circle
*/
void Cgpathaddrelativearc (cgmutablepathref path, const cgaffinetransform * __nullable Matrix, cgfloat x, cgfloat y, CGFlo At radius, cgfloat startangle, CGFloat delta);
Appends an arc arc to a path that is an arc (x1,y1) to (x2,y2) tangent
void Cgpathaddarctopoint (Cgmutablepathref path,const cgaffinetransform * m, cgfloat x1, cgfloat y1, CGFloat x2, CGFloat y2 , cgfloat radius);
Append a path to a path
void Cgpathaddpath (Cgmutablepathref path1,const cgaffinetransform * m, cgpathref path2);
The sample code is as follows:
-(void) DrawRect: (cgrect) Rect {
Cgcontextref contextref = Uigraphicsgetcurrentcontext ();
Cgpoint Center = cgpointmake (RECT.SIZE.WIDTH/2, RECT.SIZE.HEIGHT/2);
Cgmutablepathref pathref = cgpathcreatemutable ();
Cgpathmovetopoint (Pathref, Nil, center.x, center.y-50);
Cgpathaddlinetopoint (Pathref, Nil, center.x+100, CENTER.Y);
Cgpathaddquadcurvetopoint (pathref, nil, 0, 0, center.x+100, center.y-100);
Cgpathaddrelativearc (pathref, nil, 0, M_PI);
Cgpathclosesubpath (PATHREF);
[[Uicolor Redcolor] setstroke];
Cgcontextaddpath (Contextref, pathref);
Cgcontextdrawpath (Contextref, Kcgpathstroke);
Cgpathrelease (PATHREF);
Cgcontextrelease (CONTEXTREF);
}
The effect is as follows:
Iv. Summary of other methods in Cgpath
Determine if a path is empty
BOOL Cgpathisempty (cgpathref path);
Determines whether a path is a rectangle
BOOL Cgpathisrect (cgpathref cg_nullable path, cgrect * rect);
Gets the point at which a path is currently drawn
Cgpoint cgpathgetcurrentpoint (cgpathref path);
Gets the size of a path that contains all the points
Cgpathgetboundingbox (cgpathref cg_nullable path);
Get the size of a path
CGRect Cgpathgetpathboundingbox (cgpathref path);
Determine if a path contains a point
BOOL Cgpathcontainspoint (cgpathref path, const cgaffinetransform * m, Cgpoint Point, bool Eofill);
V. about the cgpathelement structural body
When each operation is made to the Cgpath path, the process of operation is actually recorded, and each operation behavior node is encapsulated in order to cgpathelement the struct, and the developer can get all the operation behavior by the following methods:
Cgpathapply (Pathref, Nil, func);
The 3rd parameter in the Cgpathapply () method is a function pointer, and the example C function is implemented as follows:
void func (void * __nullable info,
Const Cgpathelement * Element) {
printf ("%d", (*element). Type);
}
The cgpathelement structure is defined as follows:
struct Cgpathelement {
Types of Operation nodes
Cgpathelementtype type;
corresponding set of points
Cgpoint * points;
};
The Cgpathelementtype enumeration is defined as follows
typedef cf_enum (int32_t, Cgpathelementtype) {
Action behavior for moving to the point
Kcgpathelementmovetopoint,
Action behavior for adding lines
Kcgpathelementaddlinetopoint,
Operation Behavior of adding two times Bezier curves
Kcgpathelementaddquadcurvetopoint,
Operation Behavior of adding three times Bezier curves
Kcgpathelementaddcurvetopoint,
Operation Behavior of closed paths
Kcgpathelementclosesubpath
};
Original address: https://my.oschina.net/u/2340880/blog/757072
iOS Development Coregraphics Core Graphics framework one of the--cgpath applications