Stanford iOS Development Lesson Five (Part II)

Source: Internet
Author: User
Tags uikit

Reprint please indicate the source

http://blog.csdn.net/pony_maggie/article/details/27845257


Author: Pony


Five Code Demo sample

The above mentioned knowledge points are covered in this demo sample. In addition, I'm just here to analyze some of the important code, a lot of other knowledge please download the code yourself (the article has the address below) and combined with the public class to see.

Create a new single view of project, and then add a new view class. Called Faceview, for example with what is seen:


And then we drag it in the storyboard. A generic view control. As the corresponding view of the view class above, for example as seen:


Then we have to do the view controller class to add this Faceview oulet so that we can manipulate the view, such as the following:


Start looking at the code.


We are going to draw a smiley face in Faceview to reflect the program of happiness, a smiley face consists of the following parts:

The contour of a face (a great circle)

Eyes (two small circles)

Mouth (Bezier curve)

Good. The code is as follows:

-(void) Drawcircleatpoint: (Cgpoint) P Withradius: (cgfloat) Radius incontext: (cgcontextref) context{//Set as current context// Use Uikit to make random drawings. You will want to save the current Uikit context.    Contains all the already drawn content,//then switches to a new drawing context uigraphicspushcontext (context);    Cgcontextbeginpath (context);    Cgcontextaddarc (context, p.x, p.y, radius, 0, 2*M_PI, YES);    Cgcontextstrokepath (context); Uigraphicspopcontext ();} This method was called when a view was first displayed or when an event occurs that invalidates a visible part of the view Only override Drawrect:if-perform custom drawing.//an empty implementation adversely affects performance during a        nimation.-(void) DrawRect: (cgrect) rect{cgcontextref context = Uigraphicsgetcurrentcontext ();    Draw Face (circle)//draw eyes (2 circles)//no nose//mouth (Bézier curve) cgpoint midpoint; Attention here. Because the draw circle itself is based on Faceview itself. So the coordinates are also relative to the Faceview, so use the coordinates under the bounds//when we put Faceview in storyboard, and then use frame control to see the effect is very obvious midpoint.x = Self.bounds.origin.x + Self.boUNDS.SIZE.WIDTH/2;    Midpoint.y = Self.bounds.origin.y + self.bounds.size.height/2; CGFloat size = SELF.BOUNDS.SIZE.WIDTH/2;    Large Circle Radius if (Self.bounds.size.height < self.bounds.size.width) {size = SELF.BOUNDS.SIZE.HEIGHT/2;        } size *= Self.scale;    Set the line width and color cgcontextsetlinewidth (context,5.0);    [[Uicolor Bluecolor] setstroke];    [Self drawcircleatpoint:midpoint withradius:size incontext:context];    #define EYE_H 0.35#define eye_v 0.35#define Eye_radius 0.10 cgpoint Eyepoint;    Eyepoint.x = midpoint.x-size * EYE_H;    Eyepoint.y = midpoint.y-size * EYE_V;    [Self drawcircleatpoint:eyepoint withradius:size*eye_radius incontext:context];    Eyepoint.x + = size * Eye_h * 2;      [Self drawcircleatpoint:eyepoint withradius:size*eye_radius incontext:context];    #define MOUTH_H 0.45#define mouth_v 0.45#define mouth_smile 0.25//curved proportions, the degree of Smiles Cgpoint Mouthstart;    Mouthstart.x = midpoint.x-size * MOUTH_H; Mouthstart.y = Midpoint.y + siZe * MOUTH_V;    Cgpoint mouthend = Mouthstart;        Mouthend.x + = size * Mouth_h * 2;    Cgpoint mouthCP1 = Mouthstart;    mouthcp1.x + = size * Mouth_h * 2/3;    Cgpoint mouthCP2 = mouthend;        mouthcp1.x-= size * Mouth_h * 2/3;    float smile = [Self.datasource smileforfaceview:self];    if (Smile <-1) {smile =-1;    } else if (Smile > 1) {smile = 1;    } cgfloat smileoffset = mouth_smile * Size * SMILE;    Mouthcp1.y + = Smileoffset;        Mouthcp2.y + = Smileoffset;    Cgcontextbeginpath (context);    Cgcontextmovetopoint (context, mouthstart.x, MOUTHSTART.Y);    Cgcontextaddcurvetopoint (context, mouthcp1.x, Mouthcp1.y, mouthcp2.x, Mouthcp2.y, mouthend.x, MOUTHEND.Y);            Cgcontextstrokepath (context); }

Let's look at the results in the previous image:


Here's one thing to note. We need to consider the situation of horizontal screen. Because I was in the XCODE5 environment to write code, do not use the teacher into the struts and springs, directly add constraints. Let storyboard help me to calculate the initiative, for example, see below:


And then look at the effect,


This does not seem to be what we want, it is self-stretching, we also need to add some code adjustment, horizontal screen to redraw the smiley face.

-(void) setup{    //uiviewcontentmoderedraw can make the screen rotate when called drawrect    self.contentmode = Uiviewcontentmoderedraw;} -(void) awakefromnib{    [self setup];}


So let's look at the effect:


Now let's add gesture recognition. Let this smiley face support the zoom function.

The corresponding code needs to be added to the Facview and the root controller. In Facview:

Gesture recognition, zoom function-(void) Pinch: (Uipinchgesturerecognizer *) gesture{    if ((gesture.state = = uigesturerecognizerstatechanged) | |        (gesture.state = = uigesturerecognizerstateended))    {        /* The         following two lines of code are actually the same as this one, so be aware of the effect of the second line 1. To check how Gesture.scale is valued         Self.scale = Gesture.scale;         */        Self.scale *= Gesture.scale;        NSLog (@ "scale:%f", Gesture.scale);        Gesture.scale = 1;    }}

Then in the root controller:

[Self.faceview Addgesturerecognizer:[[uipinchgesturerecognizer alloc] InitWithTarget:self.faceView action:@ Selector (pinch:)];

Look at the effect:



Next we continue to join the function, we use the agent and swipe up and down gesture recognition to achieve through the swipe up and down to control the smiling face smile degree.

Gesture recognition is good understanding. Why use an agent? To understand this, the smiley face in Faceview is controlled by a happiness index (happiness in the controller). This happiness index is a data source. The Faceview is a view, and the view itself cannot have a data source, so the controller is used as the agent to manage the data source .

Here is the code.

Join gesture Recognition

Add a swipe up and down gesture. Note that target is not a faceview but a controller, so the processing function is also implemented in the controller, Uipangesturerecognizer is mainly used for dragging, that is, capturing the displacement of the finger

[Self.faceview Addgesturerecognizer:[[uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Handlehappinessgesture:)]];self.faceview.datasource = self; Set the controller as the agent

Note the following inview are followed by the specified Self.faceview as the parameters. The significance of this parameter is that you intend to convert this gesture into which coordinate system to work, of course, our faceview.

-(void) Handlehappinessgesture: (Uipangesturerecognizer *) gesture{    if (gesture.state = = uigesturerecognizerstatechanged) | |        (gesture.state = = uigesturerecognizerstateended))    {        Cgpoint translation = [gesture translationinview:self.faceview];//conversion to point displacement changes in the coordinate        system self.happiness-= TRANSLATION.Y/2; In addition to the effect of 2. Decrease the amplitude of the change        [gesture Settranslation:cgpointzero inview:self.faceview];//0 to make the range of changes not additive    }}


Next, implement the functions defined in the Protocol,

-(float) Smileforfaceview: (Faceview *) sender{    return (self.happiness-50)/50.0;//happiness is 0~100, smile degree is -1~1, Need to convert}

Where to use the proxy

float smile = [Self.datasource smileforfaceview:self];    if (Smile <-1)    {        smile =-1;    }    else if (Smile > 1)    {        smile = 1;    }

Simulator execution, you will find that with the mouse drag up and down, smiling face will change the degree of smile.


Code:

Https://github.com/pony-maggie/Happiness

Stanford iOS Development Lesson Five (Part II)

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.