Drawing in view by IOS

Source: Internet
Author: User

1. Create a viewbasedapplication

2. Add a new objective-C class and set it as a subclass of uiview. you can name it myview.

3. Rewrite the myview method.

-(Void) drawrect :( cgrect) rect

This method is defined in myview. You can override this method to display your own repainted content.

Add the following code in the method to implement gradient color

Cgcontextref context = uigraphicsgetcurrentcontext ();
Cgcolorspaceref RGB = cgcolorspacecreatedevicergb ();
Cgfloat colors [] =
{
204.0/255.0, 224.0/255.0, 244.0/255.0, 1.00,
29.0/255.0, 156.0/255.0, 215.0/255.0, 1.00,
0.0/255.0, 50.0/255.0, 126.0/255.0, 1.00,
};
Cggradientref gradient = cggradientcreatewithcolorcomponents
(RGB, colors, null, sizeof (colors)/(sizeof (colors [0]) * 4 ));
Cgcolorspacerelease (RGB );
Cgcontextdrawlineargradient (context, gradient, cgpointmake
(0.0, 0.0), cgpointmake (0.0, self. Frame. Size. Height ),
Kcggradientdrawsbeforestartlocation );

The following code draws a character on the view screen

Uicolor * magentacolor =
[Uicolor colorwithred: 0.5f
Green: 0.0f
Blue: 0.5f
ALPHA: 1.0f];
/* Set the color in the graphical context */
[Magentacolor set];
/* Load the font */
Uifont * helveticabold =
[Uifont fontwithname: @ "helveticaneue-bold"
Size: 30366f];
/* Our string to be drawn */
Nsstring * mystring = @ "Angry Bird ";
/* Draw the string using the font. The color has
Already been set */
[Mystring drawatpoint: cgpointmake (25,190)
Withfont: helveticabold];

Then draw a diagonal line on the screen.

Cgcontextref ref = uigraphicsgetcurrentcontext (); // obtain the prepared canvas. The drawing on this canvas is to draw on the current view.

Cgcontextbeginpath (REF); // a very important concept mentioned here is path. In fact, it is to tell the canvas environment that we are about to draw. You can write it down.

Cgcontextmovetopoint (ref, 0, 0); // do I need to explain how to draw a line? No, right? It is two points to determine a straight line.

Cgcontextaddlinetopoint (ref, 300,300 );

Cgfloat redcolor [4] = {1.0, 1.0 };

Cgcontextsetstrokecolor (ref, redcolor); // sets the color of the current paint brush. Paint Brush! Do you remember the Windows picture board I mentioned earlier?

Cgcontextstrokepath (REF); // tell the canvas to paint the path I moved with a paint brush.

 

Override method in viewcontroller (I am calling game01viewcontroller here. This method is automatically generated when a project is created, inherited from uiviewcontroller)

 

@ Interface game01viewcontroller: uiviewcontroller {

-(Void) loadview {
Myview * view = [[myview alloc] init];
Self. view = view;
}

Instantiate myview and set the view attribute of the Controller to the object view of myview.

 

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.