Swift TapGestureRecognizer和UIButton

时间:2018-12-19 22:57:05

标签: ios swift

我是Swift的新手,对于任何类型的错误我都感到抱歉。 在我的View Controller中,我有一个视图CanvasView,我希望用户单击按钮并在点击的位置添加形状。我创建了一个名为ShapeSquare的uiview,我有三个具有三种不同形状的按钮。不幸的是,我不明白我在哪里错。这只是代码的一部分。 任何建议将不胜感激!

@IBOutlet weak var CanvasView: CanvasView!


override func viewDidLoad() {
    super.viewDidLoad()


    let tapGR = UITapGestureRecognizer(target: self, action: Selector(("didTap")))
    tapGR.numberOfTapsRequired = 1
    CanvasView.isUserInteractionEnabled = true
    CanvasView.addGestureRecognizer(tapGR)

}
func didTap(tapGR: UITapGestureRecognizer) {
    let tapPoint = tapGR.location(in: CanvasView)
    let shapeView = ShapeSquare(origin: tapPoint)
    CanvasView.addSubview(shapeView)

}


@IBAction func SquareShape(_ sender: UIButton) {
    CanvasView.setNeedsDisplay()       
}

1 个答案:

答案 0 :(得分:-1)

1-

let tapGR = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))

2-

@objc func didTap(_ tapGR: UITapGestureRecognizer) {