IOS programming-touch event processing (2)

Source: Internet
Author: User

In the previous IOS programming-touch event processing (1), I learned how to handle touch events, events, and contacts. The first object to be touched is the view, and the uiview class of the view inherits the uirespnder class. However, to process the event, you also need to override the event processing function defined in the uiresponder class. The program calls corresponding processing functions based on the touch status. These functions include the following:

-(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;

When the finger contacts the screen, the touchesbegan: withevent method is called;

When the finger moves on the screen, the touchesmoved: withevent method is called;

When your finger leaves the screen, the touchesended: withevent method is called;

When the touch is canceled (for example, the touch is interrupted by a call), The touchescancelled: withevent method is called. When these methods are called, they exactly correspond to the four enumerated values of the phase attribute in the uitouch class.

The above four event methods do not require full implementation during the development process. You can rewrite the specific methods as needed. The four methods have two identical parameters: touches of the nsset type and event of the uievent type. Touches indicates all uitouch objects generated by the touch, and event indicates a specific event. Because uievent contains all the touch objects during the entire touch process, you can call the alltouches method to obtain all the touch objects in the event, or call touchesforview: Or touchesforwindows: extracts the touch objects in a specific view or window. In these events, you can get the touch object and perform logical processing based on its location, status, and time attribute.

For example:

-(Void) touchesended :( nsset *) touches withevent :( uievent *) event
{
Uitouch * Touch = [touches anyobject];
If (touch. tapcount = 2)
{
Self. View. backgroundcolor = [uicolor redcolor];
}
}

The preceding example shows that the background color of the current view is set based on the number of tapcount clicks after the touch finger leaves. Whether one finger or multiple fingers at a time, the tapping operation adds the tapcount of each touch object to 1, because the above example does not need to know the position or time of the specific touch object, therefore, you can directly call the touches anyobject method to obtain any touch object and then determine its tapcount value.

The detection tapcount can be placed in touchesbegan or touchesended, but the latter is usually accurate, because touchesended can ensure that all fingers have left the screen, in this way, the click action and the drag action will not be confused.

The click operation can easily lead to ambiguity. For example, after a user clicks it once, the user does not know whether the user wants to click or simply double-click it, or, after clicking it twice, you do not know whether you want to double-click or continue to click. To solve this problem, you can generally use the "latency call" function.

For example:

-(Void) touchesended :( nsset *) touches withevent :( uievent *) event
{
Uitouch * Touch = [touches anyobject];
If (touch. tapcount = 1)
{
[Self defined mselector: @ selector (setbackground :) withobject: [uicolor bluecolor] afterdelay: 2];
Self. View. backgroundcolor = [uicolor redcolor];
}
}

The above Code indicates that after the first click, the background attribute of the view is not directly changed. Instead, it is changed after being set in two seconds using the optional mselector: withobject: afterdelay: method.

-(Void) touchesended :( nsset *) touches withevent :( uievent *) event
{
Uitouch * Touch = [touches anyobject];
If (touch. tapcount = 2)
{
[Nsobject cancelpreviousperformrequestswithtarget: Self selector: @ selector (setbackground :) object: [uicolor redcolor];
Self. View. backgroundcolor = [uicolor redcolor];
}
}

Double-click is the combination of two clicks. Therefore, the method for setting the background color is enabled when you click the button for the first time. When detecting double-click, you must cancel the previous method, you can call the cancelpreviousperformrequestwithtarget: selector: Object method of the nsobject class to cancel the method call of the specified object, and then double-click the corresponding method to set the background color to red.

The following example shows how to create a drag-and-drop view, which is implemented by touching the coordinates of the object. Therefore, you can call the locationinview method of the touch object.

For example:

Cgpoint originallocation;
-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event
{
Uitouch * Touch = [touches anyobject];
Originallocation = [Touch locationinview: Self. View];
}

-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event
{
Uitouch * Touch = [touches anyobject];
Cgpoint currentlocation = [Touch locationinview: Self. View];
Cgrect frame = self. View. frame;
Frame. Origin. x + = currentlocation. x-originalLocation.x;
Frame. Origin. Y + = currentlocation. y-originalLocation.y;
Self. View. Frame = frame;
}

In touchesbegan, use [Touch locationinview: Self. view] Get the position of the finger touch on the current view, record it with the cgpoint variable, and then obtain the current position of the touch object in the touchesmoved method of the finger movement event, the offset is calculated based on the difference from the original position, and the position of the current view is set.

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.