[Object-C language essay 4] creating a view and drawing a simple image

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/iphone-object/401.html

N is busy during this time. No way, start-up companies, of course, will still learn IOS again after the time ~ OK. I will not talk about the basic language basics. From today on, I will write the game development section directly;

I declare that the iOS game development series are not as authoritative as the android game development series I wrote, because I am also learning, but I just want to share it with you. I hope you do not want to be honest, now we are all first-year students, Keke, well, don't talk much about it. Now we start to get into the question;

To draw a graph, we must first need a view, which is uiview in IOS. The following describes how to display the view on the iPhone in two ways;

First, create a project: (window-based application)

 

Create a custom gameview class that inherits the uiview class;

OK. You do not need to modify or modify anything except inheriting the uiview. The following describes two ways to display the view,

Method 1: Use the interface Builder tool to bind a view;

Double-click mainwindow. XIB of the current project to start interface builder;

Drag a view control from the library to the window (adjust the view filling screen), and modify this view to associate it with the view class of the newly created gameview,

In fact, the Association is to point the class to gameview in the "game view identity" view box, and then save it!

Method 2: add the defined uiview to the window;

(These two classes are generated by default after the project is created. Myviewtestappdelegate. H, myviewtestappdelegate. m)

Modify the myviewtestappdelegate. H class and add a custom View class:

Gameview * view;

Modify myviewtestappdelegate. m, and add the custom View class of the instance to the window view;

// Cgrect drawingarea = [Window bounds]; // determine the view size. The default value is the screen size (except for the status bar)

Cgrect square = cgrectmake (0, 0,320,480); // customize the screen size (except for the status bar)

[Window addsubview: [[gameview alloc] initwithframe: Square];

These two display views have been introduced, but they are similar. The first is to use visual operations to let the system generate the code relationship for the view added to the window, the second is that we can directly add a custom View class instance to the window through code. Of course, there is no effect at this point, because the view does not draw anything;

Next, let's draw a triangle in our gameview;

In fact, it is easy to draw. You only need to override the-(void) drawrect (cgrect) rect; function of uiview. As for how to draw a triangle, the code is provided here, which is very simple and will not be explained in detail, I also have corresponding remarks for each line of code;

Complete code of gameview. M:

//// Gameview. M // myviewtest /// created by himi on 11-7-25. // copy 2011 _ mycompanyname __. all rights reserved. // # import "gameview. H "@ implementation gameview-(void) drawrect :( cgrect) rect {// ------- the following method only applies to the drawing class provided by uikit, this method is very simple // cgrect recte = [self bounds]; // customize the drawing area (size default screen size) // [[uicolor greencolor] set]; // set the color to green // uirectfill (recte); // fill in the green area // cgrect square = cgrectmake (50, 50,100,100 ); // custom area // [[uicolor yellowcolor] set]; // set the color to yellow // uirectfill (square ); // fill in the yellow area // [[uicolor blackcolor] set]; // set the black area // uirectframe (square); // It is not filled here, draw a black border for the yellow area // ------- the following method only uses the drawing class provided by CG, this method can draw a complex drawing // fill the background with green [[uicolor greencolor] set]; uirectfill ([self bounds]); // obtain the canvas cgcontextref context = uigraphicsgetcurrentcontext () prepared by the current view; // draw the triangle cgcontextbeginpath (context) using path ); // mark cgcontextmovetopoint (context, 50, 30); // set the starting point cgcontextaddlinetopoint (context, 10,150); cgcontextaddlinetopoint (context, 100,150); cgcontextclosepath (context ); // indicates the end of the path. If this parameter is left blank, [[uicolor yellowcolor] setfill]; [uicolor blackcolor] setstroke]; cgcontextdrawpath (context, kcgpathfillstroke) is disabled by default. // draw Path} @ end

Project:

OK. This chapter will introduce you here, all of which are--Go home .. Keke;

Source code: http://www.himigame.com/iphone-object/401.html

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.