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
}
}
}
Related
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
How to identify in swift if which button was pressed i have 3 button actions.now i have another button is which when pressed it will identify which button from the 3 was pressed. cause first is you have to click from the 3 button after clicking from the 3 button then you will click
idenfifywhichpressed button and this button will identify or print which button from the 3 was pressed.
//this button will identify
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
the 3 buttons
#IBAction func btn1(_ sender: UIButton) {
}
#IBAction func btn2(_ sender: UIButton) {
}
#IBAction func btn3(_ sender: UIButton) {
}
Try something like this
Declare an enum
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
Button Action
Connect the three button actions to this method with different tag
#IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
Small improvement on #prajnaranjan-das answer: casting to an enum immediately to clean up some cruft and remove the need to implement default in the switch...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
This is helped for me (swift 5.0)
#IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}
Do something like this,
var index:Int = 0
Now do this, FYI, set button tags accordingly 1,2 and 3...
#IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
now this,
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
FYI. You can use Switch statement as well in idenfifywhichpressed method.
Update. Do not create three methods for btn1, btn2 and btn3, just create one method and assign tag accordingly for simplicity.
I think you can take a variable like
var checkBtn: String?
inside 1st button change the variable value
checkBtn = "1"
inside 2nd button change the variable value
checkBtn = "2"
inside 3rd button change the variable value
checkBtn = "3"
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
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
I have the following code to handle a swipe gesture.
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Right) {
print("Swipe Right")
self.performSegueWithIdentifier("eventsModally", sender: self)
}
}
What is the best practise to make this method available in the whole project, instead of implementing it in every ViewController class?
Help is very appreciated.
Put it in a class extension.
extension UIViewController {
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .Right) {
print("Swipe Right")
self.performSegueWithIdentifier("eventsModally", sender: self)
}
}
}
I have a screen with 6 buttons. All the buttons are connected to one IBAction. They are tagged and I use a switch statement to determine which one was tapped.
How can I add a Long and Tap gesture to each button? So for example when I tap button 1 it know if its a long gesture or a tap gesture?
So if I tap the button will do something different then when I long press.
Thanks.
#IBAction func playPauseAudioButton(sender: UIButton) {
switch sender.tag {
case 1:
//Tap Gesture
//Long Gesture
//I need this for every button
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
Do it like this
#IBAction func playPauseAudioButton(sender: AnyObject) {
let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap:")
let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
tapGesture.numberOfTapsRequired = 1
sender.addGestureRecognizer(tapGesture)
sender.addGestureRecognizer(longGesture)
}
func normalTap(sender : UIGestureRecognizer) {
let recognizer: UIGestureRecognizer = sender
let tag: Int = recognizer.view!.tag
switch tag {
case 1:
// Do some action for button 1
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
print("4")
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
func longTap(sender : UIGestureRecognizer) {
let recognizer: UIGestureRecognizer = sender
let tag: Int = recognizer.view!.tag
if sender.state == .Ended {
print("UIGestureRecognizerStateEnded")
//Do Whatever You want on End of Gesture
}
else if sender.state == .Began {
print("UIGestureRecognizerStateBegan.")
//Do Whatever You want on Began of Gesture
}
switch tag {
case 1:
// Do some action for button 1
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
print("4")
case 5:
print("5")
case 6:
print("6")
default:
print("Default")
}
}
Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.
You can set each Gesture Recognizer to different IBActions in the interface builder.
#IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}
#IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}
Through code without interface builder
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}
func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}
Hope this helps you.