如何将触摸绘制的路径与给定的贝塞尔曲线路径进行比较?

时间:2019-06-11 08:50:06

标签: ios swift

我正在iOS中开发简单的儿童字母绘图应用程序。我创建了带虚线字母的贝塞尔曲线。如果孩子画出贝塞尔曲线,我必须检测他们画的正确与否。我允许他们只在那条路径上绘制。我不知道要检测他们是否正确绘制以及他们是否正确完成了绘制。

// Created layer for path

let font = UIFont.systemFont(ofSize: 350)

var unichars = [UniChar](selectedText.utf16)
var glyphs = [CGGlyph](repeating: 0, count: unichars.count)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)

if gotGlyphs {
    let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)!
    var inverse = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -cgpath.boundingBox.height - 1)
    let path = UIBezierPath(cgPath: cgpath.copy(using: &inverse)!)

    shapeLayer.path = path.cgPath
    shapeLayer.fillColor = UIColor.clear.cgColor

    shapeLayer.strokeColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
    shapeLayer.lineDashPattern = [7,3]

    mainImageView.layer.addSublayer(shapeLayer)
}

//Allowing user to draw only over letter path

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    guard let point =  touch?.location(in: self.mainImageView) else {
        return
    }
    if let path = shapeLayer.path, path.contains(point) {
        lastPoint = point
    }
}

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

    // 1
    UIGraphicsBeginImageContext(drawPadView.frame.size)
    let context = UIGraphicsGetCurrentContext()
    tempImgView.image?.draw(in: CGRect(x: 0, y: 0, width: drawPadView.frame.size.width, height: drawPadView.frame.size.height))

    // 2
    context?.move(to: toPoint)
    context?.addLine(to: toPoint)
    // 3

    context?.setLineCap(.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(UIColor(displayP3Red:  red, green: green, blue: blue, alpha: 1.0).cgColor)
    context?.setBlendMode(.normal)

    // 4
    context?.strokePath()
    // 5
    tempImgView.image = UIGraphicsGetImageFromCurrentImageContext()
    tempImgView.alpha = opacity
    UIGraphicsEndImageContext()    
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    let touch = touches.first
    guard let point =  touch?.location(in: self.mainImageView) else {
        return
    }
    if let path = shapeLayer.path, path.contains(point) {
        drawLineFrom(fromPoint: lastPoint, toPoint: point)
        lastPoint = point
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }

    // Merge tempImageView into mainImageView
    UIGraphicsBeginImageContext(drawPadView.frame.size)
    mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawPadView.frame.size.width, height: drawPadView.frame.size.height), blendMode: .normal, alpha: opacity)
    tempImgView.image?.draw(in: CGRect(x: 0, y: 0, width: drawPadView.frame.size.width, height: drawPadView.frame.size.height), blendMode: .normal, alpha: opacity)
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

请建议我如何验证用户完成的图纸,并检查他们是否正确绘制。

0 个答案:

没有答案