[IOS UI Advanced-3.0] basic handling of touch events

Source: Internet
Author: User

A. What needs to be mastered and practiced1. Description of Event Type
2. Event handling via button event handling
3. Responder Object--uiresponder--UIView
4.view Drag and drop
* Implement Touch method, print view
* Introduction of touches and uievent parameters
* Extension: Where to go?
5. Graffiti
6. Gesture Unlock
7. Generation and transmission of events
8. Responder chain 9. Gestures B. Concept1.iOS has 3 main events
    • Touch events
    • Accelerometer Events
    • Remote control Events
2. Responder ObjectNot all objects in iOS can handle events, only objects that inherit Uiresponder can receive and handle events. What we call "Responder objects" uiapplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are all responder objects that can receive and handle events 3.UIResponderUiresponder internally provides the following methods to handle events
Touch events
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;
-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event;

Accelerometer Events
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event;
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event;
-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event;

Remote control Events
-(void) Remotecontrolreceivedwithevent: (Uievent *) event; 5.UIView Touch Event handlingUIView is a subclass of Uiresponder that can handle different touch events with the following 4 methods
One or more fingers start to touch the view, the system will automatically call the following method of view
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

One or more fingers moving on the view, the system automatically calls the view's following method (which continues to be called as the finger moves)
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event

One or more fingers to leave the view, the system will automatically call the following method of view
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event

A system event, such as a phone call, interrupts the touch process before the touch is finished, and the system automatically calls the following method of view
-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event

Tip: Uitouch objects are stored in the touches 6.UITouchWhen the user touches the screen with one finger, a Uitouch object associated with the finger is created

One finger corresponds to a Uitouch object

The role of Uitouch
Holds information about the finger, such as the location, time, and stage of the touch

When the finger moves, the system updates the same Uitouch object so that it can keep the finger in the touch position

When the finger leaves the screen, the system destroys the corresponding Uitouch object

Tip: To avoid using double-click events in iphone development! 7.UITouch PropertiesThe window where the touch is generated
@property (Nonatomic,readonly,retain) UIWindow *window;

The view at which the touch was generated
@property (Nonatomic,readonly,retain) UIView *view;

The number of times you tap the screen in a short time can be judged by Tapcount click, double tap or more
@property (nonatomic,readonly) Nsuinteger Tapcount;

Records the time when a touch event is generated or changed, in seconds
@property (nonatomic,readonly) nstimeinterval timestamp;

The state of the current touch event
@property (nonatomic,readonly) uitouchphase phase; 8.UITouch Method-(Cgpoint) Locationinview: (UIView *) view;
The return value indicates the position of the touch on the view
The position returned here is for the view's coordinate system (in the upper left corner of the view is the origin (0, 0))
When called, the view parameter is nil, which returns the position of the touch point in the UIWindow.

-(Cgpoint) Previouslocationinview: (UIView *) view;
This method records the position of the previous touch point 9.UIEventEach occurrence of an event produces a Uievent object

Uievent: Called an event object to record the time and type of event generation

Common Properties
Event Type
@property (nonatomic,readonly) uieventtype type;
@property (nonatomic,readonly) Uieventsubtype subtype;

Time the event was generated
@property (nonatomic,readonly) nstimeinterval timestamp;

Uievent also provides a way to get a touch object on a view (Uitouch) 10.touches and Event parametersA complete touch process that goes through 3 states:
Touch Start:-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
Touch move:-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
Touch End:-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
Touch Cancel (May experience):-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) event

4 Touch Event handling methods, all with Nsset *touches and uievent *event two parameters
Once a complete touch process, only one event object is generated, and 4 touch methods are the same as the same event parameter

If two fingers touch a view at the same time, view will only call once Touchesbegan:withevent: method, touches parameter is loaded with 2 Uitouch objects

If these two fingers touch the same view one after the other, then view will call 2 times Touchesbegan:withevent: Method, and only one Uitouch object is included in the touches parameter of each call

Depending on the number of Uitouch in the touches, you can tell whether it's a single touch or multi-touch. 11. Generation and transmission of events 12. UIView that can be draggedUIView color blocks that can be dragged
1 //2 //moveview.m3 //TouchMove4 //5 //Created by Hellovoidworld on 15/1/10.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "MoveView.h"Ten  One @implementationMoveview A  - /** being dragged*/ -- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event { the     //get the current touch object -Uitouch *touch =[touches anyobject]; -     -     //Previous Location +Cgpoint PREP =[Touch previouslocationinview:self]; -     //position now +Cgpoint p =[Touch locationinview:self]; A     //now the X coordinate atCGFloat x = self.frame.origin.x + (p.x-prep.x); -     //now the Y coordinate -CGFloat y = self.frame.origin.y + (P.Y-prep.y); -     //Change Position -Self.frame =CGRectMake (x, Y, Self.frame.size.width, self.frame.size.height); - } in  - @end to  

[IOS UI Advanced-3.0] basic handling of touch events

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.