How to disable swipe gesture recognizer? - ios

I have swipe gesture
#IBAction func rightSwipe(_ sender: UISwipeGestureRecognizer) {
self.view.backgroundColor = UIColor.green
}
I want to temporarily disable this swipe using long press.
#IBAction func LongPress(_ sender: UILongPressGestureRecognizer) {
}
How can I do it?

Use yourSwipeGestureRecognizer.isEnabled = false

Related

UITapGestureRecognizer is not invoked when I clicked button with IBAction

I have UITextField and UIButton in my application.
To hide system keyboard which is shown when UITextField is clicked, I added UITapGestureRecognizer to my view.
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
}
This code worked very well when I touched outside of my button.
However, when I clicked the button which has IBAction(onClickedButton), the keyboard did not disappear and only the message "aaa" printed in output console.
What I want to do is to hide keyboard and invoke IBAction at the same time. In other words, I want to invoke Tap gesture and IBAction at the same time, when I clicked my button.
How can I acheive this?
I found the solution.
Just setting
tapGesture.cancelsTouchesInView = false
can acheive this.
By doing like this, tapGesture hides keyboard and after that, passes touch event to the UIButton.
You can just add view.endEditing(true) to your onClickedButton.
try this
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGesture)
}
#objc func didTap(_ recognizer: UIGestureRecognizer) {
view.endEditing(true)
}
#IBAction func onClickedButton(_ sender: Any) {
print("aaa")
view.endEditing(true)
}
}

How to segue when a button is held in a TableView cell

I have a pretty complex problem where the button already has a tap action as an #objc function. I likely have to implement code to the cellForRow function but how would it only call when these functions are at play?
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
//Was trying to figure out how to add segue call here
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.expandButton.addGestureRecognizer(longPress)
}
you could add a segue in your storyboard from the ViewController the table is in to the View you want to go to. Then give it an identifier and call func performSegue(withIdentifier identifier: String, sender: Any?)
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
performSegue(withIdentifier: String "showFromLongPress", sender: nil)
}
}
Here of course replace the identifier showFromLongPress with the identifier you specified in the storyboard.
Apple Developer Documentation

Change view controller using swipe gesture

I want to use swipe gesture to move to another view controller using right direction. Can you help me?
#IBAction func right(_ sender: UISwipeGestureRecognizer) {
// what I must write here
}
Just write one line code -
#IBAction func right(_ sender: UISwipeGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
self.performSegueWithIdentifier("nextView", sender: self)
break
default:
break
}
}
}

Detect touch in UICollectionView

I need to stop autorotatation, if user touches to photo slider.
I use UITapGestureRecognizer to detect touch at UICollectionView:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapCollectionView(_:)))
collectionView.addGestureRecognizer(tapGesture)
}
func tapCollectionView(_ sender: UITapGestureRecognizer) {
print("touch")
}
It works when I tap on collection view.
But when I'm touching collection view and scrolling this collection view, my function 'tapCollectionView' is not called.
It helped me:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// Stop your timer here
timer?.invalidate()
}
}

UIButton with single press and long press events swift

I want to trigger two action on button click and button long click. I have add a UIbutton in my interface builder. How can i trigger two action using IBAction can somebody tell me how to archive this ?
this is the code i have used for a button click
#IBAction func buttonPressed (sender: UIButton) {
....
}
can i use this method or do i have to use another method for long click ?
If you want to perform any action with single tap you and long press the 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 method for single button and you just need Outlet for that button for that..
#IBOutlet weak var countButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
addLongPressGesture()
}
#IBAction func countAction(_ sender: UIButton) {
print("Single Tap")
}
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1.5
self.countButton.addGestureRecognizer(longPress)
}
Why not create a custom UIButton class, create a protocol and let the button send back the info to delegte. Something like this:
//create your button using a factory (it'll be easier of course)
//For example you could have a variable in the custom class to have a unique identifier, or just use the tag property)
func createButtonWithInfo(buttonInfo: [String: Any]) -> CustomUIButton {
let button = UIButton(type: .custom)
button.tapDelegate = self
/*
Add gesture recognizers to the button as well as any other info in the buttonInfo
*/
return button
}
func buttonDelegateReceivedTapGestureRecognizerFrom(button: CustomUIButton){
//Whatever you want to do
}

Resources