Add tap gesture to UIStackView

点点圈 提交于 2020-02-21 09:38:27

问题


Im attempting to add a UITapGesture to a UIStackView within a collectionView cell but each time I do my app crashes. (All IBOutlets are connected) This there something I'm doing wrong here?

 let fGuesture = UITapGestureRecognizer(target: self, action: #selector(self.showF(_:)))
 cell.fstackView.addGestureRecognizer(fGuesture)

 func showF(sender: AnyObject){
        print(111)
    }

回答1:


Is the stackview enabled for touch ? Try to set Userinteraction Parameter

cell.fstackView.isUserInteractionEnabled = true 



回答2:


If the app is crashing due to fatal error: unexpectedly found nil while unwrapping an Optional value - you may need to add some delay before adding the gesture recognizer to the stack view.

If you are accessing the stack view property in the collection view cell subclass's init or in the collection view's delegate methods, the stack view may not have been initialized yet. Try using a timer or GCD to add a 0.1 second delay and it should work fine...

Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(prepareStackView), userInfo: nil, repeats: false)

...

@objc func prepareStackView() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(stackViewTapped))
    myStackView.addGestureRecognizer(tap)
}


来源:https://stackoverflow.com/questions/37193868/add-tap-gesture-to-uistackview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!