[Stanford] Hapiness

Source: Internet
Author: User

FROM:VIEW6-VIEW7 (00:17)

1.Introduction:

Implement-Gestures:pinch and pan.

We first implement the pinch. We Add delegate to Faceview (the view), allowed Faceview to get, the Data,which is the degree ( -1 to 1) of the Smile face . Then the Controller can set self as the delegate, and provide the Smile degree ( -1 to 1) for view using the model.

Another thing is adding the pan UpDown gesture to control the happiness.

2. Visual Effects:

Pan:fig (1), fig (2), fig (3)

Pinch:fig (4)

3. The codes:

The type to create the Faceview must be the Obj-c class type under Cocoatouch, created as a UIView subclass

//faceView.h#import<UIKit/UIKit.h>@classFaceview;//just tell the compiler, Faceview class is present, for protocol can also have such as @protocol Faceviewdatasource;@protocolFaceviewdatasource<nsobject>//delegate Faceview's smiley face to anyone who wants to set it up.-(float) Smileforfaceview: (faceview*) sender;//in the method of entrustment, when we get the data and entrust something to another, we almost all pass it on, because there is no need to go back to Faceview to ask. So whenever you do a delegate or a data source, you pass yourself as a sender in the past, similar to the target action. //since this sentence uses Faceview, and Faceview has not been defined before, it must be faceview with @class; Declare the class (that is, forward reference). @end@interfaceFaceview:uiview@property (nonatomic) cgfloat scale; //Scale is scaled to achieve the zoom gesture, scaling and laughter are not related, so gesture processing is placed in the view, so that other controllers can also use my view zoom function. -(void) Pinch: (Uipinchgesturerecognizer *) gesture;//gesture handling is public because all people who use faceview know that it has the zoom function@property (nonatomic,weak) IboutletID<FaceViewDataSource> DataSource;//If someone wants to control the smile level, they have to set themselves as Faceview data source.@end//faceview.m#import "faceView.h"@implementationFaceview@synthesizeDatasource=_datasource;@synthesizescale=_scale;#defineDefault_scale 0.90//the size of the smiley face is the 90% of the view short edge size-(cgfloat) scale//Getter{       if(!_scale) {returnDefault_scale;} Else{return_scale;}}-(void) Setscale: (cgfloat) scale//Setter{          if(Scale!=_scale)//The IF condition indicates that scale has changed. To be more efficient, redraw automatically only when scale changes{_scale=Scale ;        [Self setneedsdisplay]; }}-(void) Pinch: (Uipinchgesturerecognizer *) gesture//gesture processing (this requires that the zoom gesture be added to the view, added in the controller, processed in the view, added to the setter of the Faceview outlet), scaled processing, and the option key test on the simulator {          if((gesture.state = = uigesturerecognizerstatechanged) | | (Gesture.state = =uigesturerecognizerstateended)) {Self.scale*=Gesture.scale; Gesture.scale=1;//reset the scale to 1 so that each comparison is the last change        } }-(void) setup{Self.contentmode=uiviewcontentmoderedraw;//this sentence must be in initWithFrame and awakefromnib, and after the controller is automatically rotated yes, it can be rotated before redrawing. }//In fact, there is a simple way not to write code, is to select the view in the storyboard, and then on the right attributes Inspector mode in the redraw can be selected.
-(void) awakefromnib{[self setup];}- (ID) initWithFrame: (CGRect) frame//model automatically starts with initWithFrame, making sure that what you write here is put in awakefromnib. {//create UIView objects with code, initialize them with initWithFrame, create UIView objects with Xib and do other initialization work with awakefromnib.awakefromnib is used in UIView, Viewdidload is in Uiviewcontroller Self=[Super Initwithframe:frame]; if(self) {//self.contentmode = Uiviewcontentmoderedraw;//does not work because the initwithframe is not called after the view leaves storyboard, so it is called in the awakefromnib using the Setup method. This will redraw when the view is changed, whether it's calling Init or outside the view[self setup]; } returnSelf ;} -(void) Drawcircleatpoint: (Cgpoint) P Withradius: (cgfloat) Radius incontext: (cgcontextref) context{Uigraphicspushcont Ext (context); //subroutine before modifying the context to Pushcontext, in the end to pop back. can do anything in the middle, such as changing the color of the line, so that does not destroy the outside context. Cgcontextbeginpath (context); Cgcontextaddarc (context, p.x, p.y, radius,0,2*M_PI, YES);//start angle 0, end angle 2pai, yes indicates clockwiseCgcontextstrokepath (context); //The line fill color or line width is not set here and needs to be set in place of the call, i.e. DrawRectuigraphicspopcontext ();}-(void) DrawRect: (cgrect) rect{cgcontextref context=uigraphicsgetcurrentcontext ();//Contex is required to invoke other drawing methods. //draw face [Circle]Cgpoint midpoint; Midpoint.x=self.bounds.origin.x+self.bounds.size.width/2;//Midpoint is the center point of the entire view.Midpoint.y=self.bounds.origin.y+self.bounds.size.height/2; CGFloat size=self.bounds.size.width/2;//find the short side of the view if(self.bounds.size.height<self.bounds.size.width) Size=self.bounds.size.height/2; Size*= Self.scale;//The smiley face size starts with the 90% size of the short edge of the entire view, and becomes smaller with the size of the scale.cgcontextsetlinewidth (Context,5.0);//Set line width[[Uicolor Bluecolor]setstroke]; //Set Line Color[self drawcircleatpoint:midpoint withradius:size incontext:context]; //draw a large circle to represent the contour of a face//Draw eyes [2 circle] #defineEye_h 0.30#defineEye_v 0.30#defineEye_radius 0.10Cgpoint Eyepoint; //The center point of the left eyeEyepoint.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;//Right Eye
[self drawcircleatpoint:eyepoint withradius:size*Eye_radius Incontext:context]; //No nose//Draw mouth, draw mouth with Bezier curves, draw a line between two points, then adjust the line through a control point #defineMouth_h 0.45#defineMouth_v 0.40#defineMouth_smile 0.25Cgpoint Mouthstart; //the left point of the mouthMouthstart.x= Midpoint.x-size *Mouth_h; Mouthstart.y= midpoint.y + Size *Mouth_v; Cgpoint Mouthend= Mouthstart;//the right point of the mouthmouthend.x+ = size * Mouth_h *2; Cgpoint mouthCP1=mouthstart;//Control point 1mouthcp1.x+ = size * Mouth_h *2/3; Cgpoint mouthCP2=mouthend;//Control point 2mouthcp2.x-= size * Mouth_h *2/3; //float smile=1.0; //smile represents the move control point, 0 means in the middle, 1 means the control point moves down, because the following is an addition operation. So 1.0 means smiley,-1.0 is crying floatSmile = [Self.datasource smileforfaceview:self];//using Delegates if(smile<-1) Smile =-1; 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);//move to the left start pointcgcontextaddcurvetopoint (context, mouthcp1.x, Mouthcp1.y, mouthcp2.x, Mouthcp2.y, mouthend.x, MOUTHEND.Y); Cgcontextstrokepath (context); //Strokes}@end
//HappinessViewController.h#import<UIKit/UIKit.h>@interfaceHappinessviewcontroller:uiviewcontroller@property (nonatomic)inthappiness;//0 is sad,100 The happiness in the very happy.model is different from the way the view is expressed, you can demonstrate how the controller translates the model to the view listening@end //HAPPINESSVIEWCONTROLLER.M#import "HappinessViewController.h" #import "faceView.h"@interfaceHappinessviewcontroller () <FaceViewDataSource>//Private implementation of this Protocol@property (nonatomic,weak) iboutlet Faceview*faceview;//with Faceview (model), the controller creates a outlet and then it can be dragged out of the storyboard. (Drag a generic view in storyboard to fill the entire storyboard, change its class to Faceview in the identity Inspector, Then the view in the Stroryboard is Faceview) (notice that the Stroryboard view has a yellow icon on it to represent the controller)@end@implementationHappinessviewcontroller@synthesizehappiness=_happiness;@synthesizefaceview=_faceview;-(void) Sethappiness: (int) Happiness//model change, redraw view, the method to be implemented{_happiness=Happiness; [Self.faceview Setneedsdisplay]; //Once the happiness is set, the Faceview will be redrawn. Because our view is a reflection of happiness. }-(void) Setfaceview: (Faceview *) Faceview//add gesture recognition to the setter! This is the best time to place gesture recognition when the system connects the Faceview to the controller. {_faceview=Faceview; [Self.faceview Addgesturerecognizer:[[uipinchgesturerecognizer alloc] InitWithTarget:self.faceView action:@ Selector (pinch:)]; //the target here is the handler for this gesture, the Self.faceview .[Self.faceview Addgesturerecognizer:[[uipangesturerecognizer alloc]initwithtarget:self Action: @selector (     Handlehappinessgesture:)]; Self.faceView.dataSource=self;//the controller sets itself as the delegate}          //adding gesture recognition to Faceview,faceview will handle gestures through pinch. -(void) Handlehappinessgesture: (uipangesturerecognizer*) Gesture {if((gesture.state==uigesturerecognizerstatechanged) | | (gesture.state==uigesturerecognizerstateended)) {Cgpoint translation=[gesture TranslationInView:self.faceView]; Self.happiness-= translation.y/2;         [Gesture Settranslation:cgpointzero InView:self.faceView]; }}-(float) Smileforfaceview: (Faceview *) sender//The Controller,part of its job was to interpret the data in the model, for the views. 
{
return(self.happiness- -)/50.0;//Smiley Level-1 to 1,the model ' s happiness is 0-100, this sentence is conversion}-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation//implement auto-rotation, but run discovery does not call drawrect redraw, need to set initWithFrame method in FACEVIEW.M{ returnYES;}@end

[Stanford] Hapiness

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.