IOS development gesture usage

Source: Internet
Author: User

First, in order to prevent the people who read my blog from shutting down just after they open it, let's give an example of a gesture.

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];    tapGesture.numberOfTapsRequired = 1;    tapGesture.numberOfTouchesRequired = 1;    [self.view addGestureRecognizer:tapGesture];

Speaking of this, if we talk about examples and how to use functions directly, it's not very interesting, and this kind of thing becomes a word record. If you know the mechanism, you don't need to memorize words, no matter how it changes. The previous blog talked about the channels and sequence of events. So this time we will talk about how to make some settings to meet special needs under such a big premise.

First, let's talk about the relationship between Gesture Recognition and touch events and the target for processing gestures. For example:



When you touch the screen, there will be a touchbegin event, then the touch can be moved, there will be a touchmove, and then there will be a touchend time when you leave. In general, the touch event has three phases: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140417/2014041709211089.png" alt = "\">
First, touchbegin. Then, if the user moves, the user enters touchmove. Then, when the finger leaves, the user will have touchend. the user can also directly Leave and rush touchbegin to tauchend. No matter which stage, ios will package these stages into a touch event, which is first sent to the gestures. The gestures will analyze these events, change their status, and then send a message to the target, target calls the processing function for event processing. What is changes in gesture status. View


This figure shows the State Transition Mechanism of the gesture. On the left side is a Discontinuous single gesture, and on the right side is a continuous gesture. The gesture is in the possible state by default at the beginning. When the analysis shows that it can be recognized, a single separation gesture immediately changes to the recognized State and sends a message to the target. If it is not recognized, it becomes Failed, but no message is sent to target. The continuous gesture changes from possible to begin during the first recognition, then sends a message to the target, analyzes the touch event, changes it, and sends a message to the target, when the mode does not conform to this gesture after the change, it changes to the canceled state, but does not send a message to the target. If the touch end time comes, it changes to recognized and sends a message to the target. In short, a message is sent to the target when the status changes. If the status of the target is canceled or failed, no message is sent to the target. In addition, when the status is in the recognized (end) status, ios will reset the gesture status to the possible status, but will not send a message to the target. After talking about this, we will talk about how to meet specific needs. Let's take a look at the proxy functions of the two gestures.

GestureRecognizer: shouldReceiveTouch:

GestureRecognizerShouldBegin:

GestureRecognizer: shouldReceiveTouch is the proxy that asks the gesture before the iOS access passes the touch event and asks whether to pass the touch time to the gesture. The default value is YES, indicating that the gesture is transmitted. If the value is NO, it indicates that the gesture state cannot be changed. That is to say, the gesture has never happened.

GestureRecognizerShouldBegin, what is the purpose of this function? After you add a gesture and a touchend event to a view, you can control it so that your touchend can be matched first, when the touchend is different, you can make a judgment in this function to let it return YES, And the gesture can be used again, which changes the touch event and gesture priority. However, this does not mean that the current touch event will be passed to the gesture when the touch time is passed to the view, so that the gesture will not be filtered when the touch time is passed to the view.

How can we resolve gesture conflicts?

When the recognition part of A view's A gesture is the child part of B's gesture recognition, A will first recognize it by default, and B will not be able to recognize it. How can this problem be solved?

[A requireGestureRecognizerToFail: B];

In this case, A checks the status of B before identifying the event. If B is not in the Fail status, if not, it waits, and A is still in the possible status. When B is Failed, it starts to identify.

In this way, the above requirements can be met.

We all know that when the touch event occurs, ios will send the event to the view gestures, but the first and second are not the same. By default, the first recognized gesture will block the propagation. What if I want two gestures to be recognized at the same time?

GestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer

When we need to respond to the gesture, we do not respond to B, but we need to respond to A when responding to B. What should we do?

CanPreventGestureRecognizer

CanBePreventedByGestureRecognizer:

These two functions will show one-way dependency between two gestures, [A canPreventedByGestureRecognizer: B];

In response to the gesture, B is not returned, but

The use of this part is based on whether events are transmitted to gestures.

Next, after an event is transmitted to a gesture, filter the event before the gesture fails.

View does not recognize any touch events when I need to make sure that my gestures do not fail. What should I do?

DelaysTouchesBegan is set to NO by default. If it is set to YES, it indicates that the view cannot receive the touch in event before the gesture fails.

Second, when iOS sends a touch end, the gesture is still not recognized, but it is also in a failed State. What should we do if we want the view to receive the touch end event?

Set delaysTouchesEnded to NO (YES by default)

In summary, the transfer path and sequence of events have been defined by ios, so the above method is based on this, and some events will be notified before a touch event occurs, after some things. In short, when analyzing gestures, you must always think about the spread of touch events.

Well, the use of gestures is almost the same. I hope you can give more comments.




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.