iOS animation for iOS Uibezierpath class introduction

Source: Internet
Author: User

Thank you: http://blog.csdn.net/crayondeng/article/details/11093689

Use the Uibezierpath class to create a vector-based path, which is in Uikit. This class is a package of the core graphics framework about path. Use this class to define a simple shape, such as an ellipse or a rectangle, or a shape that consists of multiple lines and curved segments.

The     1.bezier Path base    uibezierpath object is an encapsulation of the CGPATHREF data type. Path if it is based on a vector shape, it is created with straight lines and curved segments. We use straight segments to create rectangles and polygons, using curved segments to create arcs (ARC), circles, or other complex curve shapes. Each paragraph includes one or more points, and the drawing commands define how to interpret the points. The end of each line segment or curve segment is where the next begins. Each connected line or set of curved segments becomes subpath. A Uibezierpath object defines a complete path that includes one or more subpaths.      the process of creating and using a Path object is separate. Creating a path is the first step, including the steps: (1) Create a Bezier path object. (2) How to use moveToPoint: To set the starting point of the initial line segment. (3) Add line or curve to define one or more subpaths. (4) Change the properties of the Uibezierpath object associated with the drawing. For example, we can set the properties of stroked path linewidth and Linejoinstyle. You can also set the filled Path property usesevenoddfillrule.      when creating a path, we should manage the points on the path above the origin (0,0) so that we can easily move the path later. To draw the path object, we use the stroke and fill methods. These methods render the line and curve segments of path in the current graphic context.   2, using Uibezierpath to create a polygon---Add a line bar below path to form polygon polygons are simple shapes that are made up of straight lines that we can use moveToPoint: and Addlinetopoint: Method to build. Method moveToPoint: Set the starting point where we want to create the shape. From this point on, we can use the method Addlinetopoint: To create a segment of a shape. We can create line in succession, each line starting point is the previous end, the end is the specified point.   The following code describes how to create a Pentagon with a line segment. The fifth line is obtained by calling the Closepath method, which connects to the lastPoint (0,40) and first point (100,0) Description: The Closepath method not only ends a shape's subpath representation, it also draws a segment between the last point and the first point, and if we draw a polygon, this is a convenient way to draw the last line.  
//Only override Drawrect:if perform custom drawing. //An empty implementation adversely affects performance during animation. - (void) DrawRect: (cgrect) rect {uicolor*color =[Uicolor Redcolor]; [ColorSet];//Set Line ColorUibezierpath* Apath =[Uibezierpath Bezierpath]; Apath.linewidth=5.0; Apath.linecapstyle= Kcglinecapround;//Line CornersApath.linejoinstyle = Kcglinecapround;//Endpoint Processing//Set The starting point of the shape. [Apath Movetopoint:cgpointmake (100.0,0.0)]; //Draw the Lines[Apath Addlinetopoint:cgpointmake (200.0,40.0)]; [Apath Addlinetopoint:cgpointmake ( the, $)]; [Apath Addlinetopoint:cgpointmake (40.0, $)]; [Apath Addlinetopoint:cgpointmake (0.0,40.0)]; [Apath Closepath];//the fifth line is obtained by calling the Closepath method.[Apath stroke];//draws line according to the coordinate point connection}

The results of the operation are as follows:

If you modify the last line of code: [Apathfill]; The result is as follows:

So you know the difference between the stroke and the Fill method! 3. Use Uibezierpath to create a rectangle using this method:
+ (Uibezierpath *) Bezierpathwithrect: (cgrect) rect  

Demo Code:

- (void) DrawRect: (cgrect) rect {uicolor*color =[Uicolor Redcolor]; [ColorSet];//Set Line ColorUibezierpath* Apath = [Uibezierpath bezierpathwithrect:cgrectmake ( -, -, -, -)]; Apath.linewidth=5.0; Apath.linecapstyle= Kcglinecapround;//Line CornersApath.linejoinstyle = Kcglinecapround;//Endpoint Processing[Apath stroke]; }  

4. Use Uibezierpath to create a circle or ellipse using this method:
+ (Uibezierpath *) Bezierpathwithovalinrect: (cgrect) rect  
This method draws an inner tangent curve based on the incoming rect rectangle parameter. When the incoming rect is a square, the drawn image is a inscribed circle; When the incoming rect is a rectangle, the drawn image is an inner-tangent ellipse. 5. Use Uibezierpath to create an arc using this method:
+ (Uibezierpath *) Bezierpathwitharccenter: (cgpoint) Center radius: (cgfloat) Radius startangle: (cgfloat) startangle Endangle: ( CGFloat) Endangle clockwise: (BOOL) Clockwise Parameters Center Specifies the center point of the Circle (inchThe current coordinate system) used to define the arc.  RADIUS specifies the radius of the circle used to define the arc. STARTANGLE specifies the starting angle of the arc (measuredinchradians). ENDANGLE Specifies the end angle of the arc (measuredinchradians). Clockwise the directioninchwhich to draw the arc. Return Value ANewPathObjectWith the specified arc.
The parameters specify: the center of the arc, the radius, the start angle, the end angle, whether the clockwise direction. As a reference system for arcs.
Demo Code:
#define pi 3.14159265359  #define   Degrees_to_radians (DEGREES)  (pi * DEGREES)/180 )  

- (void) DrawRect: (cgrect) rect {uicolor*color =[Uicolor Redcolor]; [ColorSet];//Set Line ColorUibezierpath* Apath = [Uibezierpath bezierpathwitharccenter:cgpointmake ( Max, Max) Radius: thestartangle:0Endangle:degrees_to_radians (135) Clockwise:yes]; Apath.linewidth=5.0; Apath.linecapstyle= Kcglinecapround;//Line CornersApath.linejoinstyle = Kcglinecapround;//Endpoint Processing[Apath stroke]; }  
Results such as:


6. The Uibezierpath class provides support for adding two Bezier curves and three Bezier curves. The curve segment starts at the current point and ends at the specified point. The shape of the curve has a start point, an end point, and a tangent definition for one or more control points. Shows the similarity of the two curve types, as well as the relationship between the control point and the curve shape. (1) Draw two times Bezier curve using this method:
-(void) Addquadcurvetopoint: (cgpoint) endPoint controlpoint: (cgpoint) ControlPoint  Parameters  EndPoint  The end point of the curve.  ControlPoint the control point of the  curve.  


Demo Code:

- (void) DrawRect: (cgrect) rect {uicolor*color =[Uicolor Redcolor]; [ColorSet];//Set Line ColorUibezierpath* Apath =[Uibezierpath Bezierpath]; Apath.linewidth=5.0; Apath.linecapstyle= Kcglinecapround;//Line CornersApath.linejoinstyle = Kcglinecapround;//Endpoint Processing[Apath Movetopoint:cgpointmake ( -, -)]; [Apath Addquadcurvetopoint:cgpointmake ( -, -) Controlpoint:cgpointmake ( -,0)];  [Apath stroke]; }  


(2) Draw three times Bezier curve using this method:
-(void) Addcurvetopoint: (cgpoint) endPoint controlPoint1: (cgpoint) controlPoint1 ControlPoint2: ( Cgpoint) ControlPoint2  Parameters endPoint The end point of the  curve.  ControlPoint1 the first control point to use when  computing the curve.  ControlPoint2 the  second control   point to use when computing the curve.


Demo Code:

- (void) DrawRect: (cgrect) rect {uicolor*color =[Uicolor Redcolor]; [ColorSet];//Set Line ColorUibezierpath* Apath =[Uibezierpath Bezierpath]; Apath.linewidth=5.0; Apath.linecapstyle= Kcglinecapround;//Line CornersApath.linejoinstyle = Kcglinecapround;//Endpoint Processing[Apath Movetopoint:cgpointmake ( -, -)]; [Apath Addcurvetopoint:cgpointmake ( $, -) Controlpoint1:cgpointmake ( the,0) Controlpoint2:cgpointmake ( the, -)];  [Apath stroke]; }  




iOS animation for iOS Uibezierpath class introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.