why does GestureRecognizer not work on textview first time? - ios

I have a textView to input some text or emotion. so I make another button to change the keyboard to custom emotion view, and I also use tap GestureRecognizer for when I want to change back to keyboard.
I found every time I need to touch the button twice, the tap GestureRecognizer can work well. I thought and search long time but no result, finally I fix it. I think some one will meet the same question, so I shared it below.

because my add tap gesture recognition 's code is in init code, but when I tap textview, keyboard was pop up, the textview's position was changed too, so the init tap gesture recognition code isn't work now.
I fix it with add tap gesture recognition delay 0.3 s.

Related

How to delay start touch event of a UISlider in iOS?

In my application, there is a UISlider which is only slidable after a long-press (let's say, after 0.1 seconds of the press). To prevent from the slider to interact immediately, I set mySlider.isUserInteractionEnabled = false as default. However, since .isUserInteractionEnabled will disable all the functions, including the long-press gesture, I use another transparent UIButton of the same size as the slider on the top of it to detect long-presses, and set mySlider.isUserInteractionEnabled = true in the callback action. This works fine, except that the UIButton must be released first and then the user tap the slider again to slide it instead of drag the slider directly within the same touch. This make the slider very inconvenient to use. Therefore, I would like to know if either:
There is a better way other than mine to make UISlider start sliding only after certain delay? I'm quite surprised that I couldn't find any solution on this. I thought this would be a common need for a slider to be locked at a certain point.
or
Following my solution, is there any way that I could start dragging the slider thumb within the same touch?
Thanks a lot for any kind of answer.
Added: My solution looks like the following: in the TableViewCell, there is a label, a slider, and an invisible button with the same size as the slider (The grey background area). I set slider attribute isUserInteractionEnabled == false, and then add gesture recogniser to manipulate the slider (such as single tap, double tap for my custom functions, and long-press gesture which enable the slider). After the button is long-pressed, the isUserInteractionEnabled of the slider will set to true until the thumb goes to the new value.
Basically I only want to disable the slider to respond too quickly for short taps, the other features of the slider would remain the same. I suppose there may be better ways to achieve this, the so far using an transparent button is the only way I could think of.
You can disable user interaction on slider and then add a gesture recogniser over it (Not exactly sure about user interaction though). Maybe do something like this
let longPress = UILongPressGestureRecognizer(target: self.slider, action: Selector("longPressAndSlide:"))
longPress.minimumPressDuration = 0
self.addGestureRecognizer(longPress)
In longPressAndSlide you can calculate the direction and delta of movement and set the value of slider accordingly.
This has been answer multiple times actually. just a better search would help you more.
Update
Check answers under - How to enable "tap and slide" in a UISlider?
Latest Update
As the comments suggest, we can add a button on top of the slider and add longPress gesture over it. The handling can be done in the handler Action for the gesture.

How to add long press functionality to a UITextField

I added a UILongPressGestureRecognizer to a UITextField only to find that the only thing that got triggered was the UITextField's magnifying glass; and then when the magnifying glass disappeared the context menu appeared.
What do I have to do in order for my UILongPressGestureRecognizer to be triggered instead of the default behavior?
I've already tried disabling all UILongPressGestureRecognizers before adding my own, as per some other SO answers, but to no avail.
Put a UIView on top of the textField with the same frame. Make it clear background. Add your gestures to your UIView and handle custom selections from there on. Make a tap gesture to start editing the text manually. And make a longpressgesture to do whatever you want. etc.

Get UITextView Gesture (To Identify Location of Tap/LongPress)

I'm rather confident [editable] UITextView's become firstResponder when a long press or tap gesture occurs within the scrollView. I want to identify where in the view this touch occured. Digging through the documentation and source code didn't yield me much. I might be going about this wrong. My concern is a race condition if I just add my own tap recognizer (how can I be sure it is called before the textView's delegate methods).
For practical clarification, I want to call two similar functions from a delegate function (editingDidBegin) but depending if they touched the left or right half of the text view, I want to call either of the two.

Capturing combined button press and pan gesture

I need to capture the following touch events on iOS and interpret them:
Finger A touch down within a UIButton (stays down)
Finger B performs a pan gesture elsewhere on the screen, providing continuous callbacks. (I plan to use a UIPanGestureRecognizer to implement this functionality).
Finger A touch up within the same UIButton
In essence, pressing and holding the UIButton puts the app into a special mode which lasts only as long as the button is held. Pan gestures performed during this time do different things to when the button is not pressed.
What I've tried so far:
hooking up the UIButton Touch Down and Touch Up Inside to IBActions in my UIViewController subclass. I've also
Problem encountered: the Touch Up Inside action is not called when another gesture happens on the screen from another finger while the button is pressed. This is because the original touch is cancelled.
attaching a UITapGestureRecognizer to the UIButton
Problem encountered: This only fires when the finger leaves the screen, hence it cannot be used to put the app into a special mode while the button is pressed.
I need to use a UIButton rather than a UIView so that the button has correct highlighting behaviour when pressed.
What is the best overall approach, given the problems I've encountered so far?
Use UILongPressGestureRecognizer with short minimumPressDuration on the "button"
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/cl/UILongPressGestureRecognizer
Important thing is to use gesture recognizer delegate to make sure that gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: returns YES for these recognizers. Then, when your UIPanGestureRecognizer calls the event handler, you can check the state of your UILongPressGestureRecognizer and if the button isn't pressed just ignore the pan gesture.

UITableView - move cell - How to enter dragging mode without long press?

I am using the standard UITableView editingMode to move Cells via Drag & Drop. Works like a charm, perfectly integrated to my Core Data Model and everything.
However, usability-wise I dislike that the user has to long-press the Editing Accessory (|||). I would like to change the minimum duration of the UILongPressGestureRecognizer to something like 0.1f.
The trouble: I cant seem to access the right Gesture Recognizer. UITableViewCell's gestureRecognizers-array is empty, the UITableView's gestureRecognizers array contains only private recognizers:
UIScrollViewDelayedTouchesBeganGestureRecognizer
UIScrollViewPanGestureRecognizer
UISwipeGestureRecognizer
UIGobblerGestureRecognizer
I looked at several github-Projects:
https://github.com/bvogelzang/BVReorderTableView
https://github.com/FlorianMielke/FMMoveTableView https://github.com/mystcolor/JTGestureBasedTableViewDemo https://github.com/shusta/ReorderingTableViewController
They all focus on re-engineering UITableView so you dont have to access the built in editing mode - and instead can long press any UITableViewCell anywhere WITHOUT entering editing mode.
As I simply want to change the minimumPressDuration of the built in editing mode (and am actually fine with restricting the "access point" for the drag gesture to the Accessory View) I am reluctant to use these custom implementations potentially prone to errors and trouble.
Looking forward to your help! Thank you!!
Cheers,
Chris
You may want to access the UITableViewReorderControl, as discussed in this article, and then look for any attached gesture recognizers. If you find any, you should then be able to change the minimumPressDuration property.
Swift 5
You can change the minimumPressDuration to 0. So that you can enter drag and drop mode without a long press.
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
gesture.minimumPressDuration = 0

Resources