IOS programming-touch event processing (1)

Source: Internet
Author: User
Tags ipad keyboard

The iPhone/iPad keyboard-less design is to win more display space for the screen. The large screen provides a better user experience in viewing pictures, text, and videos. The touch screen is the main method for iOS devices to accept user input, including clicking, double-clicking, moving, and multi-touch. All these operations generate a touch event.

 

In cocoa, the class that represents the touch object is uitouch. When the user's touch screen is behind the scenes, corresponding events will occur, and all related uitouch objects will be packaged in the event and processed by programs by specific objects. The uitouch object directly contains the details of the touch.

The uitouch class contains five attributes:

Window: the window in which the touch is generated. Because the window may change, the current window is not necessarily the first window.
View: view where the touch is generated. Because the view may change, the current view is not necessarily the original view.
Tapcount: the tap operation is similar to the mouse clicking operation. tapcount indicates the number of times the screen is tapped in a short time. Therefore, you can click, double-click, or click more based on tapcount.
Timestamp: The timestamp records the time when a touch event is generated or changed. The Unit is seconds.
Phase: A Touch event has a cycle on the screen, that is, touch start, touch point movement, touch end, and midway cancel. Phase allows you to view the status of the current touch event in a cycle. Phase is of the uitouchphase type, which is an enumeration type and contains

· Uitouchphasebegan (start with touch)

· Uitouchphasemoved (touch point Movement)

· Uitouchphasestationary (the touchpoint is not moved)

· Uitouchphaseended (touch end)

· Uitouchphasecancelled (touch canceled)

The uitouch class contains the following member functions:

-(Cgpoint) locationinview :( uiview *) view: the function returns a value of the cgpoint type, indicating the position of the touch on the view. The returned position is for the coordinate system of the view. If the input view parameter is null during the call, the returned touch point is in the position of the entire window.

-(Cgpoint) previuslocationinview :( uiview *) view: This method records the previous coordinate value, and the function returns a value of the cgpoint type,
The position of the touch on the view. The position returned here is for the coordinate system of the view. If the input view parameter is null during the call, the returned touch point is in the position of the entire window.

When your fingers touch the screen, whether it's single point of touch or multi-point touch, the event starts until all your fingers exit the screen. During this period, all uitouch objects are included in the uievent event object and are distributed to the handler by the program. Events record changes in the state of all touch objects in this cycle.

As long as the screen is touched, the system will report several Touch Information encapsulated into the uievent object and send it to the program. The Management Program uiapplication object will distribute the event. In general, the event will be sent to the main window and then sent to the first responder object (firstresponder) for processing.

The concepts of the responder are described as follows:

Response
Object)

The response object can respond to the event and process the event. In iOS, The uiresponder class exists, which defines all the methods of the responder object. Uiapplication, uiview, and other classes all inherit the uiresponder class. The controls in uiwindow and uikit inherit the uiview class, so they also inherit the uiresponder class indirectly. The instances of these classes can be used as the responder.

First responder)

The current receiver object is called the first responder, indicating that the object is currently interacting with the user. It is the beginning of the responder chain.

Responder
Chain)

The responder chain represents a series of responder objects. The event is handled by the first responder object. If the first responder is not handled, the event is passed up along the responder chain and handed over to the next responder ). Generally, the first responder is a view object or its subclass object. When it is touched, the event is handled by it. If it is not processed, the event will be passed to its view controller object (if any), then its parent view (superview) object (if any), and so on until the top-level view. Next we will follow the top view (top
View) to the window (uiwindow object) to the Program (uiapplication object ). If the entire process does not respond to this event, the event will be discarded. In general, in the responder chain, as long as the event is processed by the object, the event will stop being transmitted. However, you can determine whether to continue to pass events based on certain conditions in the view response method.

Manage event Distribution

You can set the userinteractionenabled attribute of the view to respond to a touch event. The default status is yes. If it is set to no, the view can prevent the view from receiving and distributing touch events. In addition, events are not received when the view is hidden (sethidden: Yes) or transparent (the Alpha value is 0. However, this attribute is only valid for the view. If you want the entire program to respond to events in a step, you can call the beginingnoringinteractionevents method of uiapplication to completely stop event receipt and distribution. Use the endingnoringinteractionevents method to restore events that are received and distributed by programs.

If you want a view to receive multi-point touch, you need to set its multipletouchenabled attribute to yes. The default value is no, that is, the view does not receive multi-point touch by default.

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.