在绘图应用程序上绘制第一个点?

Drawing the very first dot on a drawing app?

当我想在 canvas 上打点时,它没有出现。即使我进行了一次触摸,程序也好像没有收到第一个 CGPoint 值。只有当我移动手指时,点值才会出现(例如:(190.0, 375.5), (135, 234), ...)

DV.swift

class DV: UIView {
var lines: [Line] = []
var firstPoint: CGPoint!
var lastPoint: CGPoint!

required init?(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)!
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    lastPoint = touches.first!.locationInView(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    var newPoint = touches.first!.locationInView(self)

    lines.append(Line(start: lastPoint, end: newPoint))
    lastPoint = newPoint

    self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
    var context = UIGraphicsGetCurrentContext()
    CGContextBeginPath(context)
   // print("fine") starts at beginning only

    for line in lines {

        CGContextMoveToPoint(context,line.start.x , line.start.y)
        CGContextAddLineToPoint(context, line.end.x, line.end.y)

    }
    CGContextSetRGBFillColor(context, 0, 0, 0, 1)
    CGContextSetLineCap(context, .Round)
    CGContextSetLineWidth(context, 5)
    CGContextStrokePath(context)
    }
}

Line.swift // My line initializer

class Line {
var start: CGPoint
var end: CGPoint

init(start _start: CGPoint, end _end: CGPoint) {
    start = _start
    end = _end
}
}

你只使用了touchesBegantouchesMoved,而不是touchesEnded,所以如果触摸没有移动然后结束你基本上忽略它。您需要实施 touchesEnded 以提交绘图更改并绘制它们。