"We all love Paul Hegarty." Stanford IOS8 public class personal note 19 to add gestures to the demo

Source: Internet
Author: User

In this word we will apply the knowledge learned to the demo to add gesture recognition, first add a zoom function, followed by adding a drag function, so that the villain's expression can be changed with our fingers.

First, to add a recognizer for the Faceview, we came to the code of the Scal, which previously defined an attribute that was used to represent the scale of the expression, and now I need a non-private method to modify the value:

Func scale (Gesture:uipinchgesturerecognizer) {        if gesture.state = =. Changed {        scal *= gesture.scale            gesture.scale = 1        }        }


Don't forget to finally reset the gesture scale to 1 because I want my scale to be based on the last scale.

Then go back to our controller (Happinessviewcontroller) code and modify the following:

@IBOutlet weak var faceview:faceview! {        didset{        faceview.datasource = self        faceview.addgesturerecognizer (Uipinchgesturerecognizer (target: Faceview, Action: "Scale:"))        }    }


You can see that we just added a line in the previous code. Now to run a try, there is a question of how I simulate two fingers on the simulator, I have only one mouse.

The answer is to hold down the option key, when you simulate two-finger movements, and the two fingers are relative to the center. Now I can zoom in.

Amplification:

Narrow:

Now that we continue, you can see that adding a gesture recognition is very simple. Now to add a drag gesture recognizer, I want the villain to become happy when I drag upward, and the words down will become sad. We continue to add code in the controller, still in the Faceview property Observer:

Faceview.addgesturerecognizer (Uipangesturerecognizer (target:self, Action: "Changehappiness:"))


This time there is a difference, because Target chooses the controller itself, because we want to modify the value of the model, so target should choose the controller. Then add the code for the Changehappiness method:

Of course this is to demonstrate how to make a difference with a recognizer, in fact we do not need this line of code, we can add the recognizer directly in the storyboard! So now delete this line of code, come to storyboard, find the recognizer, is these blue.

Then drag a pan recognizer into our view, and you'll find that the dragged-down recognizer disappears at once and where does it go?

There are two places where you can find the recognizer you just placed, one in outline view:

The other is at the top of the view:

Now all you have to do is let the Recognizer Cup-and-saucer controller handle the same as other controls, using a federated view and dragging it into the controller code:

So what are we going to do in this way? We want to interpret the coordinate transformation of the finger up and down into the mouth arc of the villain, which once again acts as an explanatory role, explaining to the model what is happening to the view now. This corresponds to the view interpretation model we talked about earlier, which is a mutual process, and the controller is the middle role.

Method Code:

@IBAction func changehappiness (gesture:uipangesturerecognizer) {        switch gesture.state{case        . Ended:fallthrough case        . Changed: let            translation = Gesture.translationinview (faceview) let            happinesschanged =-int (TRANSLATION.Y/ Constants.happinessgesturescale)            if happinesschanged! = 0 {            Happiness + = happinesschanged            Gesture.settranslation (Cgpointzero, Inview:faceview)            }        default:break        }    }

We do not want a little smile of the movement of the villain's expression has too much influence, so we on the gesture drag the value of the value in addition to a values, this is defined in the structure, so we can adjust it according to our own needs:

    Private struct Constants {        static let Happinessgesturescale:cgfloat = 4        }


Now run and try it, not only can drag, zoom gesture can still be recognized, the system can easily identify different gestures.

"We all love Paul Hegarty." Stanford IOS8 public class personal note 19 to add gestures to the demo

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.