How to add long press functionality to a UITextField - ios

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.

Related

How to disable interaction on NSTextAttachment?

In a UITextView with editable = YES and selectable = NO, I want to disable interaction on NSTextAttachments. To be more specific, I don't want long presses on NSTextAttachments to highlight the attachment images. I want these long press gestures to be passed to UITextView as normal text selection gestures.
What I have tried:
textView:shouldInteractWithTextAttachment:inRange: can prevent the action sheet but can not prevent the temporary highlight of the attachment image nor can it make these long presses be handled as text selection gestures.
UITextView has several installed gesture recognisers. There is one that is a subclass of UITapGestureRecognizer which you can disable.

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

why does GestureRecognizer not work on textview first time?

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.

UIPanGestureRecognizer passes the event down to a button underneath

I have a side panel that has a UIImageView to which I attached a UIPanGestureRecognizer so that you could push/pull the sidebar.
It works well.
Thing is that I have some buttons that happen to sometimes occur underneath that sidebar. If I pull it and a button is underneath, the button would fire simultaneously with the Pan.
I am not sure what the problem is, so I find it hard to solve.
Why does my UIImageView pass the UIPanGestureRecognizer event on down the chain?
Problem solved with delegation.
I disable the events on toucheBegan if a BOOL called isPanning is set to YES for all buttons.
The protocol defines only one function:
-(void)setPanning:(BOOL)isPanning;
in the touchedBegan I check to see if the value is YES, if so I don't fire that event.
I wished it would be simpler.

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources