iOS development UI Chapter-gesture recognizer (TAP)

Source: Internet
Author: User

iOS development UI Chapter-gesture recognizer (TAP)

first, the practice of listening to touch events

If you want to listen to a touch event on a view, the previous practice is to customize a view first, and then implement the touches method of the view to implement the specific processing code inside the method

There are a few obvious drawbacks to monitoring the view touch event through the touches method

(1) Custom view required

(2) Because the touch event is monitored in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view (via proxy) by default

(3) Not easy to distinguish user's specific gesture behavior

After IOS 3.2, Apple introduced the gesture recognition feature (Gesture recognizer), which greatly simplifies developer development in touch event handling

Second, gesture recognizer

In order to complete gesture recognition, the gesture recognizer must be used----Uigesturerecognizer

With Uigesturerecognizer, you can easily identify some common gestures that users make on a view

Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures and uses its subclasses to handle specific gestures

UITapGestureRecognizer (percussion)

Uipinchgesturerecognizer (pinch, for zooming)

Uipangesturerecognizer (drag)

Uiswipegesturerecognizer (Swipe)

Uirotationgesturerecognizer (swivel)

Uilongpressgesturerecognizer (Long Press)

Third, percussion

Each gesture recognizer uses a similar set of steps, such as the following uitapgesturerecognizer:

(1) Creating a gesture Recognizer Object

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

(2) Setting the specific properties of the gesture Recognizer object

2 consecutive strokes

tap.numberoftapsrequired = 2;

It takes 2 fingers to tap together.

tap.numberoftouchesrequired = 2;

(3) Add gesture recognizer to the corresponding view

[Self.iconview Addgesturerecognizer:tap];

(4) trigger of the listening gesture

[Tap addtarget:self Action: @selector (Tapiconview:)];

Property Description:

numberoftouchesrequired//How many fingers to tap together (default is 1)

Numberoftapsrequired//number of strokes required (default = 1)

Iv. Implementation Code

1 //2 //YYVIEWCONTROLLER.M3 //02-gesture recognizer (TAP)4 //5 //Created by Apple on 14-6-18.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (Weak, nonatomic) Iboutlet Uiimageview *IconView; -  - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +     //0. Set the ImageView to be interactive ASelf.iconview.userinteractionenabled=YES; at //    //1. Create a gesture recognizer - //uitapgesturerecognizer *tap=[[uitapgesturerecognizer alloc] init]; - //    //2. Set the specific properties of the gesture recognizer - //    //set the number of strokes to double-click (by default) - //tap.numberoftapsrequired=2; - //    //setting requires a finger tap (default) in //tap.numberoftouchesrequired=1; - //    //3. Add the gesture recognizer to the corresponding view to //[Self.iconview Addgesturerecognizer:tap]; + //    //4. Triggering events for listening gestures - //[Tap addtarget:self Action: @selector (Tapview)]; the      * [Self.iconview Addgesturerecognizer:[[uitapgesturerecognizer alloc]initwithtarget:self Action: @selector ( Tapview)]; $ }Panax Notoginseng-(void) Tapview - { theNSLog (@"was clicked on"); + } A  the  + @end

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.