我想繪製一條使用核心圖形和swift的直線3但是,當touchesmoved被調用時,它會創建多行而不是單行。使用的代碼如下:在swift 3和核心圖形中畫出一條直線
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var drawingPlace: UIImageView!
var startTouch : CGPoint?
var secondTouch : CGPoint?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
startTouch = touch?.location(in: drawingPlace)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
secondTouch = touch.location(in: drawingPlace)
UIGraphicsBeginImageContext(drawingPlace.frame.size)
drawingPlace.image?.draw(in: CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height))
let bezier = UIBezierPath()
bezier.move(to: startTouch!)
bezier.addLine(to: secondTouch!)
bezier.close()
bezier.lineWidth = 4
UIColor.blue.set()
bezier.stroke()
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
drawingPlace.image = img
}
}
}
btw touchesEnd創建一個單行,但只有在用戶結束觸摸後。我希望用戶在觸摸時看到正在繪製的線條。 謝謝。
你重寫'touchesMoved'?如果是,請將該代碼添加到問題中? –
@ReinierMelian你的意思是:let touch = touches.last? – mazen
@mazen你的代碼適用於我,你錯過了你的代碼的其他東西? –