IOS development-Contacts touch events

Source: Internet
Author: User

IOS development-Contacts touch events
Touch event

When a user uses an app, various events are generated.

Events in iOS can be divided into three types:

Touch event: accelerator event: Remote Control event:
Recipient

In iOS, not all objects can process events. Only objects that inherit UIResponder can receive and process events. We call it the "response object"

UIApplication, UIViewController, and UIView are all inherited from UIResponder. Therefore, they are all responder objects that can receive and process events.

UIResponder
UIResponder provides the following internal methods to handle event 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; accelerator event-(void) motionBegan :( UIEventSubtype) motion withEvent :( UIEvent *) event;-(void) motionEnded :( UIEventSubtype) motion withEvent :( UIEvent *) event; -(void) motionCancelled :( UIEventSubtype) motion withEvent :( UIEvent *) event; remote control event-(void) remoteControlReceivedWithEvent :( UIEvent *) event;
Processing of touch events in UIView
UIView is a subclass of UIResponder. It can cover the following four methods to process one or more fingers of a different touch event and start to touch the view. The system will automatically call the following method of view-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) when one or more fingers move an event on the view, the system automatically calls the following method of the view (as the fingers move, will continuously call this method)-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) one or more fingers leave the view, the system automatically calls the following method of view-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) A system event (for example, call in) before the event ends) when the touch process is interrupted, the system automatically calls the following method of view-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event prompt: all objects stored in touches are UITouch objects.
UITouch

When you touch the screen with a finger, a UITouch object associated with the finger is created.

One finger corresponds to a UITouch object

Role of UITouch
Stores finger-related information, such as the touch position, time, and phase.

When the finger moves, the system updates the same UITouch object so that it can keep the touch position of the finger.

When the finger leaves the screen, the system will destroy the corresponding UITouch object.

Tip: iPhone development is in progress. Avoid Double-click events!
UITouch attributes

The window @ property (nonatomic, readonly, retain) UIWindow * window in which the mouse is generated; the view @ property (nonatomic, readonly, retain) UIView * view in which the mouse is generated; in a short period of time, you can click, double-click, or click More @ property (nonatomic, readonly) NSUInteger tapCount based on the number of times on the screen; the time when a touch event is generated or changed. Unit: Second @ property (nonatomic, readonly) NSTimeInterval timestamp; status of the current touch event @ property (nonatomic, readonly) UITouchPhase phase;

UITouch Method

-(CGPoint) locationInView :( UIView *) view; return value indicates the position of the touch on the view. Here, the returned position is for the coordinate system of the view (starting from the upper left corner of the view (0, 0) if the input view parameter is nil during the call, the returned result is the position of the touch point in the UIWindow-(CGPoint) previuslocationinview :( UIView *) view; this method records the position of the previous touch point.
UIEvent
Every time an event is generated, A UIEvent object, called an event object, is generated to record the time and type of the event. Common attribute event type @ property (nonatomic, readonly) UIEventType; @ property (nonatomic, readonly) UIEventSubtype subtype; event generation time @ property (nonatomic, readonly) NSTimeInterval timestamp; UIEvent also provides methods to obtain the touch object (UITouch) on a view)
Touches and event parameters
A complete touch process goes through three states: Touch start:-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event touch movement:-(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 *) in the event handling method, both NSSet * touches and UIEvent * event parameters generate only one event object during a complete touch process, the four touch methods are the same event parameter. If two fingers touch a view at the same time, the view will only call the touchesBegan: withEvent: Method once, the touches parameter contains two UITouch objects. If the two fingers touch the same view one by one, the view calls the touchesBegan: withEvent: method twice, in addition, the touches parameter for each call only contains one UITouch object. Based on the number of UITouch objects in touches, you can determine whether it is single-point touch or multi-point touch.
Touch event instance
# Import "TouchView. h "@ implementation TouchView // a complete touch process: touchesBegan-> touchesMoved-> touchesEnded/*** touch start (the finger just touched the view) */-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// event. type // randomly retrieve an object UITouch * touch = [touches anyObject]; NSLog (@ "touchesBegan -- % d", touches. count); NSLog (@ "% d", touch. tapCount);}/*** touch ing (move your finger on the view) */-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {UITouch * touch = [touches anyObject]; // current touch point CGPoint current = [touch locationInView: self]; // last touch point CGPoint previous = [touch previuslocationinview: self]; // modify the current view position (midpoint) CGPoint center = self. center; center. x + = current. x-previous. x; center. y + = current. y-previous. y; self. center = center;}/*** touch ends (finger leaves view) */-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {// UITouch * touch = [touches anyObject]; // NSLog (@ "touchesEnded -- % d", touches. count);} @ end

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.