Demo highlights of Stanford's developing apps for iOS

Source: Internet
Author: User

1. Add an objective-C class and select "super" as the uiview. This is different from the view and storyboard in the interface.
2. Connect the View Controller (iboutlet) to the newly added view.
3. Draw a smiling face in the drawrect method of the model.
A general method for circle painting is created before painting:

102849512aa0f6870b67df24e00e9fcda-2559a1e

 

1028495908b657cd858429423a4ab151c8ec36bca

View code

-(Void) drawcircleatpoint :( cgpoint) P withradius :( cgfloat) radius incontext :( cgcontextref) Context {// note symmetric closed push and pop uigraphicspushcontext (context); cgcontextbeginpath (context ); cgcontextaddarc (context, P. x, p. y, radius, 0, 2 * m_pi, yes); cgcontextstrokepath (context); uigraphicspopcontext ();}

Then start to draw smiling faces.

View code

-(Void) drawrect :( cgrect) rect {// drawing code cgcontextref content = uigraphicsgetcurrentcontext (); // The drawing starts from this line // draw face (1 circle) cgpoint midpoint; midpoint. X = self. bounds. origin. X + self. bounds. size. width/2; // The center of the screen, because self. bounds. origin is located at (0, 0 ). midpoint. y = self. bounds. origin. Y + self. bounds. size. height/2; cgfloat size = self. bounds. size. width/2; If (self. bounds. size. height <self. bounds. size. WID Th) size = self. bounds. Size. Height/2; // obtain the minimum radius Based on the landscape. Size * = self. scale; // The scale determined by getter cgcontextsetlinewidth (content, 5.0); [[uicolor bluecolor] setstroke]; [self drawcircleatpoint: midpoint withradius: Size incontext: content]; // eyes (2 circle) # define eye_h 0.35 # define eye_v 0.35 # define eye_radius 0.15 cgpoint eyepoint; eyepoint. X = midpoint. x-size * eye_h; // The Left Eye Center moves from the face center to the left and up. Eyepoint. y = midpoint. y-size * eye_v; [[uicolor blackcolor] setstroke]; [self drawcircleatpoint: eyepoint withradius: size * eye_radius incontext: content]; eyepoint. X + = size * eye_h * 2; // The center of the right eye moves from the left eye to the right to the symmetric position. [Self defined: eyepoint withradius: size * eye_radius incontext: content]; // No Nose // mouth # define mouth_h 0.45 # define mouth_v 0.40 # define defined 0.25 cgpoint mouthstart; mouthstart. X = midpoint. x-size * mouth_h; // mouthstart at the left corner of the mouth. y = midpoint. Y + size * mouth_v; cgpoint mouthend = mouthstart; mouthend. X + = size * mouth_h * 2; // The symmetric position of the right corner of the mouth on the left. cgpoint mouthcp1 = mouthstart; mouthcp1.x + = size * mouth_h * 2/3; // CP (curve point) cgpoint mouthcp2 = mouthend; cursor-= size * mouth_h * 2/3; float smile = 1; cgfloat smileoffset = mouth_smile * size * smile; // The Aspect Ratio + = smileoffset; mouthcp2.y + = smileoffset; cgcontextbeginpath (content); cgcontextmovetopoint (content, mouthstart. x, mouthstart. y); // move to the left corner of the mouth cgcontextaddcurvetopoint (content, mouthcp1.x, mouthcp2.y, mouthcp2.x, mouthcp2.y, mouthend. x, mouthend. y); [[uicolor redcolor] setstroke]; cgcontextstrokepath (content );}

4. After drawrect is implemented, awakefromnib needs to be implemented when the screen is rotated, instead of re-drawrect .(In fact, you can set the mode (redraw) through a switch in the panel.)

-(void) awakeFromNib{
Self. contentmode = uiviewcontentmoderedraw; // auto-redraw after rotation is allowed
}

But in order not only to allow execution in awakefromnib, but also in initwithframe. Cause :()
So the code is changed:

-(Void) setup {self. contentmode = uiviewcontentmoderedraw; // automatically redraw after rotation is allowed,}-(void) awakefromnib {[self setup];}-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {// initialization code [self setup];} return self ;}

5. Add setter and getter for scaling scale:

View code

@ Synthesize scale = _ scale; # define default_scale 0.90-(cgfloat) scale {If (! _ Scale) {return default_scale;} else {return _ scale;}-(void) setscale :( cgfloat) scale {If (scale! = _ Scale) {// efficient processing _ scale = scale; [self setneedsdisplay];}

6. Then add a gesture.

View code

-(Void) pinch :( uipinchgesturerecognizer *) gesture {If (gesture. state = uigesturerecognizerstatechanged | gesture. state = uigesturerecognizerstateended) {self. scale * = gesture. scale; // The scaling ratio of the view is equal to the scaling ratio of the gesture. scale = 1; // reset the gesture each time }}
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.