UIButton TapGestureRecognizer未触发

时间:2017-04-02 18:02:47

标签: ios swift uibutton

这段代码的目的是将UIView设置为屏幕上某个位置的动画。不幸的是,它根本就没有注册水龙头。我想在我的设置中将委托设置为某个东西吗?任何帮助将不胜感激!

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

2 个答案:

答案 0 :(得分:1)

您应该在视图中启用userInteraction。

view.isUserInteractionEnabled = true

答案 1 :(得分:0)

尝试在Viewcontroller中的viewDidAppear中调用{{1}}方法。