swift 向从UIViewController调用的UIView的子视图添加平移手势

f45qwnt8  于 2023-01-04  发布在  Swift
关注(0)|答案(1)|浏览(143)

我创建了一个带有子视图的自定义视图,子视图的框架原点x设置为父视图的宽度,这样它看起来就像一个突出视图的标签。(类似于选项卡文件夹)。自定义视图是视图控制器中的IBOutlet。我的目标是拖动选项卡拉出自定义视图,类似于打开面板。当我在拖动选项卡时添加Pan手势时,子视图、什么都没发生。当我直接把它添加到父视图时它就能工作了。任何帮助都会很棒。

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

func和delegate方法都不被触发。

nfeuvbwi

nfeuvbwi1#

//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

相关问题