Seven kinds of gesture summary in IOS _ios

Source: Internet
Author: User
Tags touch

Today for you to introduce iOS seven kinds of gestures, gestures in the development of often used, so it is simple and easy to understand the next, say not more, directly look at the code:

Initializes a uiimageview
uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (MB, m, +]];
Imageview.image = [UIImage imagenamed:@ "12.jpg"];
Uiimageview user interaction is closed by default, and to enable him to handle touch events, we have to manually open it
[ImageView Setuserinteractionenabled:yes];
[Self.window Addsubview:imageview];
Initializes a view (responder) to carry the gesture
/*uiview *gestureview = [[UIView alloc] Initwithframe:cgrectmake (MB, m)];
The current view is placed to the center of the screen
gestureview.center = self.window.center;
Gestureview.backgroundcolor = [Uicolor yellowcolor];
[Self.window Addsubview:gestureview];

1. Pat gesture

Create a pat gesture
uitapgesturerecognizer *TAPGR = [UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tapaction:)];

1. Set the Touch object, and pat the number of times

Set the number of touch objects (several fingers)
[TAPGR setnumberoftouchesrequired:1];
Set Pat times
[TAPGR Setnumberoftapsrequired:2];
Add gestures to the created view
[Gestureview ADDGESTURERECOGNIZER:TAPGR];
callback method for Pat gestures
-(void) Tapaction: (uitapgesturerecognizer*) sender{
//You can get a view of its current action based on gestures
Uiimageview * ImageView = (uiimageview*) Sender.view;
Get TextField Viewwithtag The return value of this method is UIView type, but Uitextfield is a subclass of UIView, the parent class object cannot directly point to the subclass object, so you need to cast the
Uitextfield * TextField = (uitextfield*) [Self.window viewwithtag:1000];
Recycle keyboard, cancel first responder
[TextField Resignfirstresponder];
NSLog (@ "I tapped the Gestureview");
}

2. Kneading gesture

Create kneading gesture
uipinchgesturerecognizer* pinchgr = [[Uipinchgesturerecognizer alloc] initwithtarget:self action:@ Selector (pinchaction:)];
Pinchgr.delegate = self; A callback method that enables multiple gesture//kneading gestures to be implemented on the same view
-(void) Pinchaction: (uipinchgesturerecognizer*) sender{
//By kneading gestures to the scaling ratio
float scale = Sender.scale;
The view that the gesture is acting
uiview *view = Sender.view;
Zoom function in 2d affine transformation function to zoom out of view//to
change the first parameter of the current view
//function: The transform value of an existing view/
/second parameter: The scaling ratio
on the x axis The third parameter: the scaling ratio on the y-axis
//is changed in the original transform state of the view, regardless of how many times it is executed, it is based on the original transform state of the view to change the
View.transform = Cgaffinetransformmakescale (2, 2);
View.transform = Cgaffinetransformscale (view.transform, scale, scale);
After each kneading action completes, the kneading value is restored so that it scales Sender.scale = 1 each time starting at 100%
.

3. Rotate gesture

Rotate gesture
uirotationgesturerecognizer* rotagr = [[Uirotationgesturerecognizer alloc] initwithtarget:self action:@ Selector (rotaaction:)];
Rotagr.delegate = self;
Rotate gesture callback method
-(void) Rotaaction: (uirotationgesturerecognizer*) sender{
//by gesture to rotation angle
float rota = sender.rotation;
The view that the action of the gesture is obtained
uiview *view = Sender.view;
The rotation function in the 2D affine transformation function is used to rotate the current view.
view.transform = Cgaffinetransformrotate (View.transform, rota);
Recovery
sender.rotation = 0;
}

4. Translation gestures

Translation gesture
Uipangesturerecognizer *pangp = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Panaction:)];
The callback method for translating gestures
-(void) Panaction: (uipangesturerecognizer*) sender{
//Gets the view of the current gesture
uiview *view = Sender.view ;
Get the offsets we move on the view
cgpoint currentpoint = [sender TranslationInView:view.superview];
Realization of view position change by means of displacement-related functions in 2D affine transformation
view.transform = Cgaffinetransformtranslate (View.transform, Currentpoint.x, CURRENTPOINT.Y);
recovery//Every time starting from 00 o'clock
[sender Settranslation:cgpointzero InView:view.superview];
}

5. Edge Light sweep gesture

Edge Light sweep gesture
Uiscreenedgepangesturerecognizer *edgepangr = [[Uiscreenedgepangesturerecognizer alloc] Initwithtarget:self Action: @selector (edgepanaction:)];
Edgepangr.edges = Uirectedgeall;
Edge Light Sweep Gesture callback method
-(void) Edgepanaction: (uiscreenedgepangesturerecognizer*) sender{
NSLog (@ "I successfully triggered screen edge gestures") ;
}

6, Long press gesture

⑥ Long Press gesture
uilongpressgesturerecognizer *LONGPRESSPR = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (longpressaction:)];
longpresspr.minimumpressduration = 1;
⑥ the callback method for long gestures
-(void) Longpressaction: (Uilongpressgesturerecognizer *) sender{
if (sender.state = = uigesturerecognizerstateended) {
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "See you Paralyzed" message:@. Don't you bite me! "delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];
[Alertview show];
}

7. Light sweep gesture

⑦ Light sweep gesture
Uiswipegesturerecognizer *swipegr = [[Uiswipegesturerecognizer alloc]initwithtarget:self action:@ Selector (swipeaction:)];
callback method for ⑦ light sweep gestures
-(void) Swipeaction: (Uiswipegesturerecognizer *) sender{
if (sender.state = = uigesturerecognizerstateended) {
Uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle:@ "Slot-ni"] Delegate:self cancelbuttontitle:@ "paper" destructivebuttontitle:@ "hahaha" otherbuttontitles:@ "切毛毛", nil];
[Actionsheet ShowInView:self.window];
}

Add gestures to the ImageView view

3. Add gestures to a picture a view can add a variety of gestures, but a gesture can only be added to a view
[ImageView ADDGESTURERECOGNIZER:TAPGR];
[ImageView ADDGESTURERECOGNIZER:PINCHGR];
[ImageView ADDGESTURERECOGNIZER:ROTAGR];
[ImageView Addgesturerecognizer:pangr];
[ImageView Addgesturerecognizer:edgepangr];
[ImageView ADDGESTURERECOGNIZER:LONGPRESSPR];
[ImageView Addgesturerecognizer:swipegr];

Use gesture proxies when you want to add multiple gestures to a view (emphasis)

Pragma mark----Proxy method for gestures
//Enables multiple gestures to respond simultaneously
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) othergesturerecognizer{
// When the return value is yes, the action of another gesture can also be performed when a gesture is performed
;

The above is a small set up to introduce the iOS seven kinds of gesture summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.