iOS touch event and gesture recognition

Source: Internet
Author: User

iOS touch event and gesture recognition

Directory

    • Overview
    • Touch events
    • Gesture recognition

Overview

To implement some new requirements, we often need to add touch events and gesture recognition to iOS

Touch events

Four ways to touch events

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event start touch triggered method

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) method triggered when event moves

-(void) touchesended: (Nsset *) touches withevent: (uievent *) method triggered when event departs

-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) The method that is called when the event system cancels touch events for some reason

How to get a touch position

For (Uitouch *t in touches) {

Cgpoint touchposition = [t locationInView:self.view];

NSLog (@ "%f,%f", TOUCHPOSITION.X,TOUCHPOSITION.Y);

}

Gesture recognition

Gesture Type

UITapGestureRecognizer light Touch

The Uiswipegesturerecognizer quickly slid

Uipangesturerecognizer drag

Uipinchgesturerecognizer two fingers pinch or put

Uirotationgesturerecognizer Finger Direction operation

Uilongpressgesturerecognizer Long Press

UITapGestureRecognizer light Touch

Initialization

UITapGestureRecognizer *TAPGR = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapaction:)];

[View ADDGESTURERECOGNIZER:TAPGR];

Common methods

[Self setnumberoftapsrequired:numbers];

[Self setnumberoftouchesrequired:number2];

Other ways to refer to Uigesturerecognizer documentation

Response method of UITapGestureRecognizer

-(void) Tapaction: (UITapGestureRecognizer *) sender{

When tap picks up,

if (tapgr.state = = uigesturerecognizerstateended) {

}

When TAP is pressed

if (tapgr.state = = Uigesturerecognizerstatebegan) {

}

Wait, other states.

}

Get the UITapGestureRecognizer pressed position

Cgpoint point = [Sender Locationinview:view];

iOS touch event and gesture recognition

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.