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 examples

The above mentioned knowledge points are covered in this example. In addition, I'm just here to analyze some important code, more knowledge please download the code yourself (the article at the bottom of the address) and combined with the public class to see.

Create a new single view project, and then add a view class called Faceview, as shown in:


And then we drag it in the storyboard. A generic view control that corresponds to the view class for the above view, as shown in:


Then to do the view controller class add this Faceview oulet so that we can manipulate the view as follows:


Started to focus on 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)

OK, here's the code:

-(void) Drawcircleatpoint: (Cgpoint) P Withradius: (cgfloat) Radius incontext: (cgcontextref) context{//Set as current context//    Using Uikit for any drawing, you would want to save the current Uikit context, including all the already drawn content,//and then switch to a completely new context in the drawing 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;    Note here, because the circle itself is based on Faceview itself, so the coordinates are calculated relative to the Faceview, so to use the coordinates of the bounds//when we put Faceview in the storyboard lira some, and then use frame contrast 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 is one thing to note, we need to consider the situation of horizontal screen, because I am in XCODE5 environment write code, do not use the teacher into struts and springs, directly add constraints, let Storyboard help me calculate automatically, as follows:


And then look at the effect,


This does not seem to be what we want, it automatically stretches, we also need to add some code adjustments, 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 to allow this smiley face to support the zoom feature.

Need to add the corresponding code in the Facview and root controller, 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. You can check how Gesture.scale is         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 add features, we use the agent and swipe up and down gesture recognition to achieve through the swipe up and down to control the smiling face of the degree.

Gesture recognition good understanding, why use Agent? It can be understood that the smiley face in Faceview is controlled by a happiness index (Controller's happiness), which is a data source, and that Faceview is a view, 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.

Add gesture Recognition

Add a swipe up and down gesture, note that target is not faceview but controller, so the processing function is also implemented in the controller, Uipangesturerecognizer is mainly used for dragging, is to capture 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 that the following inview are followed by specifying Self.faceview as the parameter, and the meaning of this parameter is what coordinate system do you intend to convert this gesture to, 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 2 of the effect, reduce the amplitude of the change        [gesture Settranslation:cgpointzero inview:self.faceview];//0 can 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;    }

Run under the simulator, you will find that the smiling face will change as the mouse drags up and down.


Code:

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

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.