iOS development using custom curves to draw complex graphics (Bezier curves) on UIView

Source: Internet
Author: User

Sometimes we need to draw a graph of irregular paths, which may contain lines or curves, and can be implemented using Uibezierpath.


The Uibezierpath class can represent any shape that can be defined with Bézier curves, and we can create our own custom curves. When you are done, you can use the resulting Uibezierpath object to fill and stroke like any other path.
1, the following demonstrates drawing an irregular graphic using Uibezierpath:
(1) The brush moves to the upper-left corner of the rectangular area
(2) Draw a straight line from the point of the pen's current position to the top right corner
(3) Draw a straight line from the point of the pen's current position to the lower-left corner
(4) Draw a curve from the pen's current position to the lower right corner, and the two control points of the curve bending degree are the midpoint of the rectangular region
(5) Draw a straight line from the point to the top left of the pen's current position, closing the path
2, the effect chart is as follows:
3, the code is as follows:

The code is as follows Copy Code
Import Uikit

Class Viewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Let Viewrect = CGRect (x:50, y:50, width:100, height:100)
Let View1 = MyCanvas (frame:viewrect)
Self.view.addSubview (View1)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Class Mycanvas:uiview {
Override Init (Frame:cgrect) {
Super.init (Frame:frame)
Set the background color to transparent
Self.backgroundcolor = Uicolor.clearcolor ()
}

Required init? (Coder Adecoder:nscoder) {
FatalError ("Init (coder:) has not been implemented")
}

Override func DrawRect (rect:cgrect) {
Let Bezierpath = Uibezierpath ()

Creates a rectangle with all of its edges indented 5%
Let Drawingrect = Cgrectinset (Self.bounds,
Self.bounds.size.width * 0.05,
Self.bounds.size.height * 0.05)

Identify the points that make up the painting
Let TopLeft = Cgpointmake (Cgrectgetminx (Drawingrect),
Cgrectgetminy (Drawingrect))

Let TopRight = Cgpointmake (Cgrectgetmaxx (Drawingrect),
Cgrectgetminy (Drawingrect))

Let BottomRight = Cgpointmake (Cgrectgetmaxx (Drawingrect),
Cgrectgetmaxy (Drawingrect))

Let Bottomleft = Cgpointmake (Cgrectgetminx (Drawingrect),
Cgrectgetmaxy (Drawingrect))

Let center = cgpointmake (Cgrectgetmidx (Drawingrect),
Cgrectgetmidy (Drawingrect))

Start drawing
Bezierpath.movetopoint (TopLeft)
Bezierpath.addlinetopoint (TopRight)
Bezierpath.addlinetopoint (Bottomleft)
Bezierpath.addcurvetopoint (BottomRight, Controlpoint1:center, Controlpoint2:center)

Closes the path and ends the drawing
Bezierpath.closepath ()

Set colors, and draw them
Uicolor.greencolor (). Setfill ()
Uicolor.blackcolor (). Setstroke ()

Bezierpath.fill ()
Bezierpath.stroke ()
}
}
Related Article

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.