我试图编写一个应用程序代码,该应用程序部分涉及使用手指跟踪照片中的对象。我有两个相关文件:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var image_edit: UIImageView!
var rec_houseimages: [UIImage]?
override func viewDidLoad() {
super.viewDidLoad()
image_edit.image = rec_houseimages?[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
和这一个:
import UIKit
class Draw: UIView {
var touch : UITouch!
var lineArray : [[CGPoint]] = [[CGPoint]()]
var index = -1
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first! as UITouch
let lastPoint = touch.location(in: self)
index += 1
lineArray.append([CGPoint]())
lineArray[index].append(lastPoint)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first! as UITouch
let currentPoint = touch.location(in: self)
self.setNeedsDisplay()
lineArray[index].append(currentPoint)
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
if(index >= 0){
context!.setLineWidth(5)
context!.setStrokeColor((UIColor(red:0.00, green:0.38, blue:0.83, alpha:1.0)).cgColor)
context!.setLineCap(.round)
var j = 0
while( j <= index ){
context!.beginPath()
var i = 0
context?.move(to: lineArray[j][0])
while(i < lineArray[j].count){
context?.addLine(to: lineArray[j][i])
i += 1
}
context!.strokePath()
j += 1
}
}
}
当我运行程序时,我将所需的图像放在屏幕上,绘图代码执行正常;但是图像位于图形的前面,因此如果您在图像外部绘制,则只能看到线条。我一直在查看诸如带上SubviewToFront之类的内容,但我不知道如何用这个绘图来实现它们。