iOS gesture operations, four basic events and six common events

Source: Internet
Author: User

Basic events include begin,canceled,move,ended four items, if the object's hidden property is yes, then no effect, hidden property must be no;

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event{//touch start

NSLog (@ "%ld", [touches count]);

if ([[Event Alltouches]count]==2) {

Nsarray * One =[[event alltouches]allobjects];

_tilabel. hidden=NO;

_ylabel. hidden=NO;

_tilabel. Center=[[one Objectatindex:0] Locationinview:self. View];

_ylabel. Center=[[one Objectatindex:1] Locationinview:self. View ];

}

}

-(void) touchescancelled: (nsset *) touches withevent: (uievent *) event{//touch unexpected termination is

_tilabel. hidden=YES; _ylabel. hidden=YES;

}

-(void) touchesended: (nsset *) touches withevent: (uievent *) event{//touch end

_tilabel. Hidden=YES; _ylabel. Hidden=YES;

}

-(void) touchesmoved: (nsset *) touches withevent: (uievent *) event//touch move

{

NSLog (@ "%ld", [[Event Alltouches]count]);

if ([[Event Alltouches]count]==2) {

Nsarray * One =[[event alltouches] allobjects];

_tilabel. hidden=NO;

_ylabel. hidden=NO;

_tilabel. Center=[[one Objectatindex:0] Locationinview:self. View ];

_ylabel. Center=[[one Objectatindex:1] Locationinview:self. View];

}

}

Here are six common events, including: Click, drag, pinch, rotate, long press and swipe

Click events: As the name implies (UITapGestureRecognizer)

Dragging events: dragging objects within view (Uipangesturerecognizer)

Pinch events: Methods used primarily for manipulating objects and shrinking (Uipinchgesturerecognizer)

Rotation events: Primarily used to control the rotation angle of an object (Uirotationgesturerecognizer)

Long-press event: As the name implies (Uilongpressgesturerecognizer)

Sweep event: The main add in the view, swipe and can be classified according to the properties of the upper and lower four-way sweep (Uiswipegesturerecognizer)

To add six events to an object, the Userinteractionenable property of the object must be yes, otherwise the six events will be unresponsive:

-(void) Viewdidload {

[Super Viewdidload];

UIImage * pic =[uiimage imagenamed:@ "rmb.jpg"];

Uiimageview * Imgview = [[Uiimageview alloc]initwithimage:p ic];

Imgview. BackgroundColor = [Uicolor blackcolor];

Imgview. frame =CGRectMake (0, 0, + );

Imgview. userinteractionenabled =YES;/********** This setting must be yes************/

[Self. Code creation of the View Addsubview:imgview];//imageview object

/*------------------------The following is the creation of the event object and the add Operation---------------------*/

// click

UITapGestureRecognizer * Tapgesturerecognizer=[[uitapgesturerecognizer alloc]initwithtarget: Self action:@selector(handletap:)];

Tapgesturerecognizer. numberoftapsrequired=1;

Tapgesturerecognizer. numberoftouchesrequired=1;

Set the number of clicks and the number of fingers for a click event

[Imgview Addgesturerecognizer:tapgesturerecognizer];

// drag

Uipangesturerecognizer * Pangesturerecognizer=[[uipangesturerecognizer alloc]initwithtarget: Self action:@selector(handlepan:)];

[Imgview Addgesturerecognizer:pangesturerecognizer];

// rotate

Uirotationgesturerecognizer * Rotationgesturerecognizer = [[Uirotationgesturerecognizer alloc] Initwithtarget:self Action:@selector(handlerotation:)];

[Imgview Addgesturerecognizer:rotationgesturerecognizer];

// kneading

Uipinchgesturerecognizer * pinchgesturerecognizer= [[Uipinchgesturerecognizer alloc]initwithtarget :self Action:@selector(handlepinch:)];

[Imgview Addgesturerecognizer:pinchgesturerecognizer];

// Long Press

Uilongpressgesturerecognizer * Longgesturerecognizer = [[Uilongpressgesturerecognizer alloc] Initwithtarget:self Action:@selector(handlelong:)];

[Imgview Addgesturerecognizer:longgesturerecognizer];

// cleaning

Uiswipegesturerecognizer * Leftgesturerecognizer = [[Uiswipegesturerecognizer alloc]initwithtarget: Self action:@selector(handleswipe:)];

Leftgesturerecognizer.direction=uiswipegesturerecognizerdirectionleft;//Set sweep direction

Uiswipegesturerecognizer * Rightgesturerecognizer = [[Uiswipegesturerecognizer alloc]initwithtarget :self Action:@selector(handleswipe:)];

Rightgesturerecognizer.direction=uiswipegesturerecognizerdirectionright;//Set sweep direction

[Self. view Addgesturerecognizer:leftgesturerecognizer];

[Self. view Addgesturerecognizer:rightgesturerecognizer];

}

-(void) Handletap: (UITapGestureRecognizer *) recognizer {

NSLog (@ " sweep left");

[[[Uialertview alloc]initwithtitle:@ ' hint ' message:@ ' Click event occurred ' Delegate:self cancelbuttontitle:nil otherbuttontitles:@ "OK", Nil] Show];

}// Click to process

-(void) Handlepan: (uipangesturerecognizer *) recognizer{

Uiimageview * current= (Uiimageview *) recognizer. View // get ImageView object

cgpoint translaation=[recognizer translationinview:recognizer. View]; // get the coordinates of the move

Current. center=Cgpointmake (current. Center. x+translaation. x, current. Center. y+translaation. y);

Assign a value to the ImageView object using the original coordinates plus the moved coordinates

[Recognizer settranslation: Cgpointzero inview:self. View];

//Clear 0 to prevent moving again

}// Drag handling

-(void) Handlerotation: (uirotationgesturerecognizer *) recognizer{

Recognizer. View . Transform=cgaffinetransformrotate(Recognizer.view. Transform, recognizer.rotation);

}// Rotation Processing

-(void) Handlepinch: (uipinchgesturerecognizer *) recognizer{

Recognizer. view. transform = cgaffinetransformscale (recognizer. View. transform, recognizer. Scale , recognizer. Scale );

recognizer. scale=1;

}// kneading Treatment

-(void) Handleswipe: (uiswipegesturerecognizer *) recognizer{

if (recognizer.direction==uiswipegesturerecognizerdirectionright) {

NSLog (@ " right sweep ");

}Else if(recognizer.direction==uiswipegesturerecognizerdirectionleft) {

NSLog (@ " sweep left ");

}

}// Cleaning Treatment

-(void) Handlelong: (uilongpressgesturerecognizer *) recognizer{

NSLog (@ " Long press event ");

}// Long Press processing

iOS gesture operations, four basic events and six common events

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.