0
private let swipeUp: UISwipeGestureRecognizer = {
    let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
    swiper.direction = .up
    return swiper
}()

private let swipeDown: UISwipeGestureRecognizer = {
    let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
    swiper.direction = .down
    return swiper
}()

creating my swipeUp and swipeDown gesture recognizers

func movedUp(sender: UISwipeGestureRecognizer){
    print("UP")
}
func movedDown(sender: UISwipeGestureRecognizer){
    print("DOWN")
}

my functions to be called when the swipe is recieved

override func viewDidLoad() {
    self.view.addGestureRecognizer(swipeUp)
    self.view.addGestureRecognizer(swipeDown)

}

adding my gesture recognizers in viewDidLoad

my program runs, but nothing happens when i swipe, what am i doing wrong? Thankyou everyone

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Artyrcheek
  • 23
  • 5

2 Answers2

0

Try this:

private lazy var swipeUp: UISwipeGestureRecognizer = {
    let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedUp))
    swiper.direction = .up
    return swiper
}()

private lazy var swipeDown: UISwipeGestureRecognizer = {
    let swiper = UISwipeGestureRecognizer(target: self, action: #selector(movedDown))
    swiper.direction = .down
    return swiper
}()

override func viewDidLoad()
{
    super.viewDidLoad()
    self.view.addGestureRecognizer(swipeUp)
    self.view.addGestureRecognizer(swipeDown)
}

func movedUp(sender: UISwipeGestureRecognizer)
{
    print("UP")
}

func movedDown(sender: UISwipeGestureRecognizer)
{
    print("DOWN")
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Please don't post just code. Explain what was wrong and how your answer fixes the problem. "Try this" is not a useful description. – rmaddy Jun 01 '17 at 17:25
  • Can't you see the difference in the code? Its the same code as yours just "let" replaced with "lazy var". And description is required when the code works according to your requirement. First try and run it then ask the questions. – PGDev Jun 01 '17 at 17:27
  • I'm offering you advice on how to write a better answer. People shouldn't have to run a diff on the two versions of code. Good answers explain the issue and explain the solution. – rmaddy Jun 01 '17 at 17:29
-1

Have you tried func movedUp(sender: UIGestureRecognizer)

Jitendra Tanwar
  • 249
  • 2
  • 11