这段代码的目的是将 UIView 动画到屏幕上的某个位置。不幸的是,它根本没有注册水龙头。我是否应该将委托(delegate)设置为我的设置中的某些内容?任何帮助将不胜感激!
public class NoteCardView:UIView {
internal var titleLabelTapGestureRecognizer: UITapGestureRecognizer?
@IBInspectable var delegate: NoteCardViewDelegate?
public func setup(positionX:CGFloat, positionY: CGFloat, view: UIView, cardValue: Int) {
self.titleLabelTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(NoteCardView.handleTitleLabelTap(_:)))
self.addGestureRecognizer(titleLabelTapGestureRecognizer!)
}
func handleTitleLabelTap(_ recognizer:UITapGestureRecognizer) {
self.delegate?.noteCardViewTitleLabelDidRecieveTap(self)
}
}
public protocol NoteCardViewDelegate {
func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView!) -> Bool
}
extension MainViewController: NoteCardViewDelegate {
func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView!) -> Bool {
let card:NoteCardView = (view)
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
card.titleLabel.leftAnchor.constraint(equalTo: card.titleLabel.leftAnchor, constant: (self.elementPlacement / 4) + 20)
card.titleLabel.bottomAnchor.constraint(equalTo: card.titleLabel.bottomAnchor, constant: 100)
}, completion: { (finished: Bool) in
self.elementPlacement = (self.elementPlacement + 1)
card.titleLabelTapGestureRecognizer!.isEnabled = !(self.elementPlacement == 5)
})
return true
}
}
最佳答案
您应该在您的 View 中启用 userInteraction。
view.isUserInteractionEnabled = true
关于ios - UIButton TapGestureRecognizer 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171676/