Stanford iOS Development Lesson Five (Part II)

Source: Internet
Author: User
Tags uikit

Reprint please indicate the source

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. Then add a view class. Called Faceview, for example with what is seen:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcg9uev9tywdnawu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

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. For example, 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)

Well, 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; Note here that because the circle itself is based on Faceview itself, the computed coordinates are also relative to the Faceview, so use the coordinates under bounds//when we put Faceview in the storyboard lira. And then using 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//bending ratio.    The degree of the Smile 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 a point to note, we need to consider the situation of horizontal screen, because I am 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 take a look at the effect.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcg9uev9tywdnawu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

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:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcg9uev9tywdnawu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

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:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcg9uev9tywdnawu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Next continue to add functionality. We use the proxy and swipe up and down gesture recognition to achieve a smile by swiping up and down to control the smiling face.

Gesture recognition good understanding, why use Agent? Be able to understand this. This smiley face in the Faceview. is controlled by a happiness index (happiness in the controller). This happiness index is a data source. That 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 what coordinate system you intend to convert the gesture to. Of course it's 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 2 effect, reduce 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;    }

Under the simulator, you will see the mouse drag up and down. The smile level of smiling faces will change.



Stanford iOS Development Lesson Five (Part II)

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: 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.