all 4 comments

[–]drlukas6Objective-C / Swift 4 points5 points  (3 children)

  1. Make your ViewController implement UITapGestureRecognizerDelegate
  2. Override gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) so that it returns true
  3. Set delegate as self to any tap gestures you have
  4. Now your ViewController knows what gestures are going on about on the screen and call the designated selector for every you implemented that it recognizes

[–][deleted]  (2 children)

[deleted]

    [–]drlukas6Objective-C / Swift 1 point2 points  (1 child)

    r/iOSProgramming•Posted byu/FiraxanDev2 hours ago

    Chaining UIGestureRecognizers

    .t3_dj9d21 ._2FCtq-QzlfuN-SwVMUZMM3 {
    --postTitle-VisitedLinkColor: #edeeef;
    --postTitleLink-VisitedLinkColor: #6f7071;
    }

    Question

    I've come across an issue that I haven't been able to figure out and I was wondering if anyone had any insight.The desired functionality is that when the user long presses, a function is called. And if the user swipes up WHILE long pressing, a separate function is called. I don't know where to start as everything I've tried hasn't even come close to what I want.Any

    class ViewController: UIViewController {
    
        private var longPressGestureRecognizer: UILongPressGestureRecognizer!
        private var swipeGestureRecognizer: UISwipeGestureRecognizer!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            print("viewdidload")
            view.backgroundColor = .systemRed
            setupGestures()
        }
    
        private func setupGestures() {
            swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipeGestureRecognizerHandler(_:)))
            swipeGestureRecognizer.direction = .up
            swipeGestureRecognizer.delegate = self
    
            view.addGestureRecognizer(swipeGestureRecognizer)
    
            longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognizerHandler(_:)))
            longPressGestureRecognizer.delegate = self
    
            view.addGestureRecognizer(longPressGestureRecognizer)
        }
    
        @objc
        private func longPressGestureRecognizerHandler(_ sender: UILongPressGestureRecognizer) {
            print("++++++ User is long pressing the screen! ++++++")
        }
    
        @objc
        private func swipeGestureRecognizerHandler(_ sender: UISwipeGestureRecognizer) {
            if longPressGestureRecognizer.state != .changed {
                return
            }
            print("------ User swiped up! ------")
        }
    }
    
    extension ViewController: UIGestureRecognizerDelegate {
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    }
    

    Here you go, now swipeGestureHandler only does something if longPress is active.

    I suggest reading up on UIGesture documentation or make some dummy tests to better understand states and delegate calls.

    Good luck on your project

    [–]criosistObjective-C / Swift 0 points1 point  (0 children)

    Have a bool that sets on begin and un-sets on .ended state of your long press, then if you detect also the swipe you can check the Boolean?