IOS (supplemental) Touch Event handling

Source: Internet
Author: User

[1] Basic concepts of events

uievent : An event that is a hardware-captured object that represents a user-operated device.

Divided into three categories: touch events, shaking events, remote control events

Touch Events: Users manipulate objects and enter data by touching the device screen. Multi-touch support with 1 to multiple touch points

UIView Touch events are supported (because they inherit from uiresponder) and multi-touch is supported.

need to define UIView subclasses, implementing touch-related methods.

touches. Began,

touches. Moved,

touches...ended ,

Touches. Canceled

[2] gestures: a regular touch.

Uitouch represents a finger touch on the screen. You can get touch time and touch position.

How to get Touch object. the touches collection contains all the potentials on the view .

What is a responder chain

A responder chain is a chain that consists of multiple responder objects;

Basic concepts of events

uievent : An event that is a hardware-captured object that represents a user-operated device.

Divided into three categories: touch events, shaking events, remote control events

Touch Events: Users manipulate objects and enter data by touching the device screen. Multi-touch support with 1 to multiple touch points

UIView Touch events are supported (because they inherit from uiresponder) and multi-touch is supported.

need to define UIView subclasses, implementing touch-related methods.

touches. Began,

touches. Moved,

touches...ended ,

Touches. Canceled

Gestures: a regular touch.

Uitouch represents a finger touch on the screen. You can get touch time and touch position.

How to get Touch object. the touches collection contains all the potentials on the view .

[3] What is a responder chain

A responder chain is a chain that consists of multiple responder objects;

Uiresponder . The responder class.

IOS all objects that respond to events (touch, shake, remote events) are responders.

The system defines an abstract parent class Uiresponder to represent the responder. Whose subclasses are respondents

The hardware detects a touch operation and gives the information to uiapplication to begin detection.

view, uiapplication, window, Viewcontroller detect all? Child views

Final confirmation of touch location, complete the response chain query process

When a responder is detected, the implementation touchesbegan:withevent: methods, that is, handling events.

If the responder does not handle the event, the event is passed down. If there is no responder processing, the touch event is discarded.

The order of event processing is the opposite of a touch detection query.
Touch? View, Viewcontroller, Window-- uiapplication

The responder chain can be interrupted. method to complete the detection query process.

properties of the View class: userinteractionenabled . Shut down to block the query process.

Code:

#import "TestView.h" #import "RootView.h" #define Krandomcolor arc4random ()%256/255.0@interface TestView () {//start touch point C Gpoint _start;}    @end @implementation testview-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame];    if (self) {self.backgroundcolor = [uicolor Redcolor]; } return self;} When you start touching an event, execute the scheduled execution event code in touch (when you start to touch, to see it)//A touch event occurs, the method executes only once-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event{//Touch random color (Krandomcolor is the random number defined in the extension) Self.backgroundcolor = [Uicolor colorwithred:krandomcolor GRE    En:krandomcolor Blue:krandomcolor alpha:1];    The first touch time coordinates _start = [[Touches anyobject] locationinview:self]; NSLog (@ "Point I change Color");} A touch event has not ended, will always call the method//did not touch finish, always touch-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{//Moving point Cgpo    int nowpoint = [[Touches anyobject] locationinview:self];    Move the point minus the start touch point cgfloat x = nowpoint.x-_start.x;    CGFloat y = nowpoint.y-_start.y; CgpointCenterPoint = Cgpointmake (self.center.x + x, Self.center.y + y);    Self.center = CenterPoint; Coordinates NSLog (@ "%@", Nsstringfromcgpoint (Nowpoint)) when printing the move;} One Touch time end, execute the method//Touch complete-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "End");} Touch time interrupted by another,//someone Disturbed-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event{} @end






IOS (supplemental) Touch Event handling

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.