Adding long press gesture recognizer to UITextView - swift

I'm trying to let the user move a text view around the screen as he presses down on it. The issue I'm getting is my gesture recognizer isn't getting called and instead the text is being selected when I press down on it. How can I disable this? I can't just disable user interaction because a tap should still let the user edit the text. My code is below
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
text.addGestureRecognizer(longPressRecognizer)
func longPressed(sender : UITextView) {
println("long press")
}

Try adding this tiny piece of code. Sometimes it makes magic happen ;)
text.userInteractionEnabled = true

Related

How to add Swipe Gesture on a view that has a Pan Gesture as well?

So I have a view with a carousel that has two views in it. I have two buttons that navigate from the two views inside the carousel. When the user is on the right view, the right button is disabled and left is enabled. When the user is on the left view, the left button is disabled and right is enabled.
However, I have a Pan gesture that allows the user to also swipe in between the two pages without using the buttons.
When I use the gesture to move in between the two views, I also need the buttons to reflect the change (either be enabled or disabled), however, when I put it on my pan gesture function it doesn't work 100% of the time as a half swipe or short swipe not strong enough to shift the pages but is strong enough to disable/enable my buttons as if the view has changed even though it hasn't.
I tried incorporating a Handle Swipe but I couldn't get the function to work.
Can anyone take a look at this code and let me know if anything is missing? Or better yet, if there is an easier solution to accomplish my goal
#objc func yesswiped (recognizer: UISwipeGestureRecognizer){
print("swipe pls")
}
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}

How can I have a button and scroll view in the same spot?

To build context, my app has a stack of cards (similar to Tinder) that flip and each could contain a lot of text on one side that require a UITextView for scrolling. It's basically a flashcard app, so once the user is done looking at the card, they swipe it away to view the next one.
I'm trying to make it so the user can tap anywhere on the card to flip it, so I added a clear button that fills up the card. My problem is that when the button is at the front, it blocks the text view from being scrollable, and when the button is at the back, the text view blocks it from being pressed.
Is there a way around this somehow to allow for both functionalities?
You can create a UITapGestureRecognizer, like so:
let gesture = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
addGestureRecognizer(gesture)
And the function to trigger:
#objc func tap(gesture: UITapGestureRecognizer) {
print("Tap!")
}
Maybe you can add a UITapGestureRecognizer to the text view itself. Something like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
textView.addGestureRecognizer(tapGesture)
#objc func didTap(_ gesture: UITapGestureRecognizer) {
// Handle the tap
}
did you try a tableView or a collectionView, you can customize your cell to look like a card then when a user clicks the cell it will flip or present a popup view, thats easier.

How to add a UILongPressGestureRecognizer release function

I am using a long press gesture recognizer because without it if I click and release quickly on the buttons, the code doesn't execute properly. But with the long press gesture recognizer, my buttonUp function does not execute. How can I check if a finger is off the screen using long press gesture recognizer?
You may refer to this if you want to have release action and hold down action in your button!
OR
You can check on the state of gesture in your long press here!
OR
Handling a long-press gesture from Apple Developer Documentation
Hope it helps.
Cheers.
If you want to perform any action with a single tap and long press you can add gestures into button this way:
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
#objc func tap() {
print("Tap happend")
}
#objc func long() {
print("Long press")
}
This way you can add multiple methods for a single button and you just need Outlet for that button for that.

Simulate Single/Double Tap Swift

Is it possible to simulate a single/double tap through Swift? I am using the Charts library from GitHub, and it has the ability to highlight an area when selected (finger pressed), but when you remove your finger, the area stays selected. I'd like to add the ability that when the finger selection is done, to simulate a single tap on the same place, or a double tap elsewhere on the graph, as that de-selects the area. Thanks!
You have to add an UITapGestureRecognizer, for example:
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.handleTap(sender:)))
chartView.addGestureRecognizer(tap)
Then you have to implement the handler:
#objc func handleTap(sender: UITapGestureRecognizer? = nil) {
// call the touch chart event here
}
After that, to simulate the tap, you just call the handleTap() function

Keyboard issue on my app

I just made a simple app where you type things in four boxes and can randomly select one. After typing in the boxes I need the keyboard to go away so people can see what the result is, but the on-screen keyboard just stays. Is this something I need to change in the files of the app?
You have to use resignFirstResponder() property of the text field.
If you want to hide the keyboard when a tap occurred outside the text field,
you can use an UITapGestureRecognizer.
override func viewDidLoad() {
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped(_:)))
view.addGestureRecognizer(tapRecognizer)
}
func backgroundTapped(sender: UITapGestureRecognizer) {
view.endEditing(true)
}