1
我想讓用戶繪製一個矩形上UIImageView
我加了兩個變量爲先的最後觸摸位置 我添加了這個功能上的UIImageView矩形: -繪製採用核芯顯卡
func draw(from: CGPoint, to: CGPoint) {
UIGraphicsBeginImageContext(imageView.frame.size)
context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
context?.setLineWidth(5.0)
let currentRect = CGRect(x: from.x,
y: from.y,
width: to.x - from.x,
height: to.y - from.y)
context?.addRect(currentRect)
context?.drawPath(using: .stroke)
context?.strokePath()
imageView.image?.draw(in: self.imageView.frame)
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
我添加該方法(touchMoved
)它吸引許多矩形
我的方法添加到(touchEnded
)它繪製一個,但是當用戶移動觸摸它不會出現
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
firstTouchLocation = touch.location(in: self.view)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
lastTouchLocation = touch.location(in: self.view)
draw(from: firstTouchLocation, to: lastTouchLocation)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
lastTouchLocation = touch.location(in: self.view)
draw(from: firstTouchLocation, to: lastTouchLocation)
}
}
我希望讓用戶擴展矩形時touchMoved
並繪製touchEnded
時。
在哪裏我可以找到關於「預測性觸摸」的信息,您知道我可以在哪裏找到一些教程或somethig? –
@ReinierMelian - 參見WWDC 2015 [iOS上的高級觸摸輸入](https://developer.apple.com/videos/play/wwdc2015/233/)。或者只是谷歌「預測教程」,你會看到很多鏈接。 – Rob
非常感謝。但它繪製了一個矩形,並且當一個新的touchBegan事件開始一個新的矩形並刪除了前一個。我想在觸摸結束後保存圖形並開始新的繪圖。 –