adding a pan gesture to a subview of a UIView called from a UIViewController

610 Views Asked by At

I created a custom view with a subview where its frame origin x is set to the width of the parent view so that it looks like a tab sticking out of the view. (like a tab folder). The custom view is an IBOutlet in a view controller. My objective is to drag the tab to pull out the custom view like opening a panel. When I add the Pan gesture to the dragging tab, the subview, nothing happens. It works when I add it directly to the parent view. Any help would be awesome.

let pan = UIPanGestureRecognizer(target: self, action: #selector(dragView(pan:)))
pan.minimumNumberOfTouches = 1
pan.delegate = self
notesView.dragView.addGestureRecognizer(pan)

neither the func nor the delegate methods are triggered.

1

There are 1 best solutions below

0
Ahmet Bostancikli On
//MARK: - Don't forget to add UIGestureRecognizerDelegate, when you add delegate to gestureRecognizer
class ViewController: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        //MARK: set panGestureRecognizer
        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerTapped))
        panGestureRecognizer.minimumNumberOfTouches = 1
        
        //MARK: set panGestureRecognizer delegate
        panGestureRecognizer.delegate = self
        
        //MARK: Set panGestureRecognizer to view
        view.addGestureRecognizer(panGestureRecognizer)
        
        //MARK: - Add user interaction to you custom view or view
        view.isUserInteractionEnabled = true
    }
    
    //MARK: when tap is happened the below attribute will be executed
    @objc func panGestureRecognizerTapped() {
        print("Pan Gesture Recognizer Tapped")
    }
    
}

//The resource code is at the below link: 
https://github.com/ahmetbostanciklioglu/PanGestureRecognizer.git