ios - UIButton TapGestureRecognizer 未触发

标签 ios swift uibutton

这段代码的目的是将 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/

相关文章:

ios - UISearchbar 在键盘出现之前做一些事情

android - Ionic 应用程序中的 "cordova already defined"

iphone - UIButton 不允许 UIScrollView 滚动

ios - 使标签栏上的自定义按钮变圆

arrays - 无法使用类型为 '[ClassName.StructName]' 的索引下标类型为 'ClassName.StructName' 的值

ios - 选择时在UIButton上添加/删除下划线

ios - 如何设置 Apple 推送通知服务中的声音? (总是默认的?)

objective-c - NS_OPTIONS 个匹配项

swift - 使用自定义相机拍照 iOS 11.0 Swift 4.更新错误

swift - 在运行第二个 Swift 之前等待代码块