【问题标题】:Creating App to create circles when users taps view创建应用程序以在用户点击视图时创建圈子
【发布时间】:2017-09-12 13:31:23
【问题描述】:

我想问是否有人能弄清楚我的代码有什么问题,当有触摸事件时,我似乎无法创建一个圆圈。

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.lightGray
    // Do any additional setup after loading the view, typically from a nib.

}

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches as? Set<UITouch> {

        let circleCenter = touch.first!.location(in: view)

        let circleWidth = CGFloat(25)
        let circleHeight = circleWidth

        let circleView = CircleView(frame: CGRect(x: circleCenter.x, y: circleCenter.y, width: circleWidth, height: circleHeight))
        view.addSubview(circleView)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

如果您需要我的子视图代码CircleView

class CircleView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.clear
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func draw(rect: CGRect) {

    var context = UIGraphicsGetCurrentContext();

    context!.setLineWidth(5.0)

    UIColor.red.set()

    let center = CGPoint(x: round(frame.size.width)/(2), y: round(frame.size.height)/2)
    let radius = (round(frame.size.width) - 10)/(2)

    context!.addArc(center:center, radius:radius, startAngle:0, endAngle:CGFloat(Double.pi) * 2, clockwise:true)

    context!.strokePath();

    }

【问题讨论】:

    标签: ios swift uitouch


    【解决方案1】:

    您的两个函数不正确(在 Swift 3 中)。改变

    func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    

    改变

    func draw(rect: CGRect)
    

    override func draw(_ rect: CGRect)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2014-08-23
      • 1970-01-01
      • 2020-07-24
      • 2011-05-04
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多