Ios uitableview click the interface to exit the keyboard implementation and some questions

Source: Internet
Author: User

It looks quite simple. I added a tap gesture to uitableview and responded to a method to exit the keyboard. So I had the following code:

// Added the tap gesture and clicked to exit the keyboard.

Uitapgesturerecognizer * tapgesture = [[uitapgesturerecognizeralloc] initwithtarget: selfaction: @ selector (dismisskeyboard)];

[Self. tableview addgesturerecognizer: tapgesture];


Hide the keyboard:

-(Void) dismisskeyboard {

[_ Textfield resignfirstresponder];

}


The problem is that I have customized uitableviewcell, and there are some buttons and other controls in it. If you click on these controls, the keyboard will not exit.

Check the information and set the cancelstouchesinview of the TAP gesture to no:

Tapgesture. cancelstouchesinview = no;

In this way, the problem is solved, but you still need to learn more about the role of the cancelstouchesinview attribute. So I checked the description of the apple DOC:

Cancelstouchesinview

A boolean value affecting whether touches are delivered to a view when a gesture is recognized.

@ Property (nonatomic) bool cancelstouchesinviewdiscussion

When this property isYES(The default) and the operator recognizes its gesture, the touches
That gesture that are pending are not delivered to the view and previusly delivered touches are canceled throughtouchesCancelled:withEvent:Message
Sent to the view. If a gesture recognizer doesn't recognize its gesture or if the value of this property isNO, The view parameters es all touches in the multi-touch sequence.

It's not easy to understand just a few simple words in English. So I checked this article on Gesture Recognition and event distribution, and found that one article was okay. I reproduced it: gesture recognizers and touch event Distribution

After reading this article, I have a basic understanding of the meaning of this article, we know that the touch event is handled by the gesture recognizers of the current view or the superview of this view. Simply put, this attribute is used to control whether gesture recognizers can eat the touch event.

For example, you have bound a click event to a button, added the tap gesture recognition to the button, and set the corresponding processing method; for the first time, the cancelstouchesinview of the TAP gesture is not set. That is to say, the default value is yes, and then click the button. The event bound to the TAP gesture is executed, and the Click Event of the button is not executed, that is, gesture
Recognizers has sent the touch event to cancel and does not pass it to the button. Therefore, the click Event of the button cannot be executed. Next, set the cancelsTouchesInView of the tap gesture to NO and click the button again, we found that both events were executed. This is clear.

However, to analyze your own problems, the scenario is not the same as the above. The above practices are all in a view, and the tap gesture is also added to this view, in my current scenario, a UITableView adds the tap gesture recognition, and the button is a subView in the UITableView, so there are the following scenarios:

Scenario 1: When cancelsTouchesInView is YES, clicking the button triggers the click Event of the button. However, the UITableView Gesture Recognition event is not triggered.

Scenario 2: When cancelsTouchesInView is NO, both events are triggered.


This is a bit strange. According to the previous understanding of gesture recognizers, scenario 1 should be triggered by the gesture recognition method, and the click Event of the button will not be triggered, because touch has been replaced by gesture
The recognizers gave it to cancel. Do I understand it wrong? Then, carefully explain the gestures in the apple doc and find the following section:

Interacting with Other User Interface Controls

In iOS 6.0 and later, default control actions prevent overlapping gesture recognizer behavior. For example, the default action for a button is a single tap. If you have a single tap
Gesture recognizer attached to a button's parent view, and the user taps the button, then the button's action method changes es the touch event instead of the gesture recognizer. this applies only to gesture recognition that overlaps the default action for
Control, which includes:

  • A single finger single tap on

    Uibutton,
    Uiswitch,
    Uistepper,
    Uisegmentedcontrol, and
    Uipagecontrol.
  • A single finger swipe on the knob of

    Uislider, in a direction parallel to the slider.
  • A single finger pan gesture on the knob of

    UISwitch, in a direction parallel to the switch.

In iOS6.0 and later versions, the default control actions will block the overlapping Gesture Recognition Behavior. For example, the default operation for a button is single tap. If you have a single tap gesture reader connected to the parent view of a button, When you click the button, it is the button operation method to receive touch events, instead of using gesture recognition, we are familiar with it. This is only applicable when the default operations of Gesture Recognition and Control overlap. The following lists some control examples.

Well, my environment is xcode4.6.2 + ios sdk 6.1, which is very important. That is to say, there are two different situations before ios6.0 and after ios6.0,

UITapGestureRecognizer of the parent view of a button on iOS 5 interferes with the click event of the button. In iOS6.0 and later versions, the click Event priority of the button is higher, it will interfere with the UITapGestureRecognizer recognition of the parent view.

The program is run under iOS 5 and iOS 6 sdks respectively for verification. The results are consistent.

Since the version is different and the effect is different, you have to deal with compatibility issues. The best solution is to set the cancelsTouchesInView of UITapGestureRecognizer to NO, so that the performance of the two versions is consistent.

Next we can give a rough explanation of this situation by version:

Versions earlier than ios6:

Uitableview is superview, and the button is subview. When you click the button, the touch event is first received by uitapgesturerecognizer on superview for recognition. If the recognition succeeds, the target of the gesture will execute the bound action, if the cancelstouchesinview of the worker is yes, the touch will be dropped by cancel and will not be passed to the button, and the Click Event of the button will not be executed. If the cancelstouchesinview of uitapgesturerecognizer is no, the Touch event is passed to the button, and the Click Event of the button is executed.

After ios6:

If the cancelstouchesinview of uitapgesturerecognizer is yes, click the button and the receiver of the touch event is the Click Event of the button,

The receiving of uitapgesturerecognizer is blocked, so the Click Event of the button is executed, but the action of the gesture is not triggered. If cancelstouchesinview is no, the event is handled by uitapgesturerecognizer first, the results are the same as those of versions earlier than ios6, so both are executed.

This is just an explanation that I have made at this stage that can be accepted for the time being. If there is a mistake or someone else has a better opinion, please leave it blank.



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.