0

I have an issue with the swipe gesture recognizer in Xcode. What I want to let the code do is the following: When you swipe upwards a view with floating buttons disappears. When you swipe downwards, the floating buttons appear again. But of course, it doesn't work :D

I can change the position of the view manually in StoryBoard without messing the constraints up.

Everything is connected properly.

It doesn't give an error message either, just doesn't work.

What can I do?

I use the following code:

import UIKit

class HomeViewController: UIViewController {


    @IBOutlet weak var BottomFloatersBottomLayoutGuide: NSLayoutConstraint!


    @IBAction func UpSwipe(_ sender: UISwipeGestureRecognizer) {
        BottomFloatersBottomLayoutGuide.constant = -400
    }

    @IBAction func DownSwipe(_ sender: UISwipeGestureRecognizer) {
               BottomFloatersBottomLayoutGuide.constant = 0
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
vandernat.p
  • 153
  • 1
  • 1
  • 10

1 Answers1

0

You have to put it in this function:

UIView.animate(withDuration: 1.0){
//your code here
}

Everything in that block will animate: change colors, a view's frame etc. So also your constrains.

Edit: Woops, I am wrong. In UIView.animate, self.view.layoutIfNeeded() needs to be called. The change of your constrains should be outside that block. layoutIfNeeded() updates the constrains.

Your code should look like this:

@IBAction func UpSwipe(_ sender: UISwipeGestureRecognizer) {
BottomFloatersBottomLayoutGuide.constant = -400
UIView.animate(withDuration: 1.0){
self.view.layoutIfNeeded()
}
}
J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • Thank you for the response. You mean like this: @IBAction func UpSwipe(_ sender: UISwipeGestureRecognizer) { UIView.animate(withDuration: 1.0){ self.BottomFloatersBottomLayoutGuide.constant = -400 } } – vandernat.p Jul 27 '17 at 13:06
  • @vandernat.p Yes! :) I hope that will work. Note: your code will not perform as you want to to be because when the user swips 2 times down, the button is far off your view... But I think you can fix that ;). – J. Doe Jul 27 '17 at 13:08
  • @vandernat.p I made a mistake, please check my edit. – J. Doe Jul 27 '17 at 13:10
  • @vandernat.p Yea my mistake, I forget to let you know you need to call layoutIfNeeded(). See my edit. – J. Doe Jul 27 '17 at 13:11
  • @vandernat.p Are you sure the IBAction function is getting called? If you print something in that function, does that print popup in your debugger? This code should work... – J. Doe Jul 27 '17 at 13:26
  • Well I used this code. But it doesn't show anything in the debugger either. Strange because as I see it it is connected to the swipe gestures in SB. @IBAction func DownSwipe(_ sender: UISwipeGestureRecognizer) { BottomFloatersBottomLayoutGuide.constant = 0 UIView.animate(withDuration: 1.0){ self.view.layoutIfNeeded() } print("Down") } – vandernat.p Jul 27 '17 at 13:36
  • @vandernat.p No, your outlets are not connected. Do it programmatically (easy) https://stackoverflow.com/a/43609633/7715250. After that, try to use the code I provided. – J. Doe Jul 27 '17 at 13:41
  • @vandernat.p Yes :)! Please mark this answer as the answer to your question by clicking the green checkmark left of the answer. Good luck! – J. Doe Jul 27 '17 at 14:04