可能是一个简单的问题。我正在尝试制作一个可以绘制直线的应用程序:水平垂直线和对角线。 我也正在尝试对线路进行测量(还不行。)
我已确定我可以画线,但不能画直线(就像没有尺子的铅笔一样) 在网上找不到任何文档来绘制直线)
import UIKit
import Foundation
class tekengedeelte: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var tekenview: UIView!
var path = UIBezierPath()
var startPoint = CGPoint()
var touchPoint = CGPoint()
var setSigImage: ((_ data: UIImage) -> ())?
var setcameraimage: ((_ data: UIImage) -> ())?
override func viewDidLoad() {
super.viewDidLoad()
tekenview.layer.shadowOpacity = 1
tekenview.layer.shadowRadius = 10
tekenview.layer.shadowOffset = CGSize(width: 1, height: 1)
tekenview.layer.shadowColor = UIColor.black.cgColor
//tekenview.layer.shadowPath = UIBezierPath(rect: tekenview.bounds).cgPath
tekenview.layer.shadowPath = CGPath(rect: tekenview.bounds, transform: nil)
tekenview.layer.shouldRasterize = true
tekenview.clipsToBounds = true
tekenview.isMultipleTouchEnabled = false
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let point = touch?.location(in: tekenview) {
startPoint = point
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let point = touch?.location(in: tekenview) {
touchPoint = point
}
path.move(to: startPoint)
path.addLine(to: touchPoint)
startPoint = touchPoint
draw()
}
func draw() {
let strokeLayer = CAShapeLayer()
strokeLayer.fillColor = UIColor.black.cgColor
strokeLayer.lineWidth = 5
strokeLayer.strokeColor = UIColor.black.cgColor
strokeLayer.path = path.cgPath
tekenview.layer.addSublayer(strokeLayer)
tekenview.setNeedsDisplay()
}
@IBAction func clearPressed(_ sender: UIButton) {
path.removeAllPoints()
tekenview.layer.sublayers = nil
tekenview.setNeedsDisplay()
}
@IBAction func setPressed(_ sender: UIButton) {
// Convert CanvasView to UIImage
let renderer = UIGraphicsImageRenderer(size: tekenview.bounds.size)
let image = renderer.image { ctx in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
// Send image back to onboarding screen
setSigImage?(image)
// Rotate device back to portrait mode
//let appDelegate = UIApplication.shared.delegate as! AppDelegate
//appDelegate.r = .portrait
// Close modal window
dismiss(animated: true)
}
//foto maken
@IBAction func camera(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func photolibraryaction(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary) {
let imagepicker = UIImagePickerController()
imagepicker.delegate = self
imagepicker.sourceType = UIImagePickerController.SourceType.photoLibrary;
imagepicker.allowsEditing = true
self.present(imagepicker, animated: true, completion: nil)
}
}
}
答案 0 :(得分:0)
Reinier Melian解释了使用核心图形绘制直线。触摸结束后,使用CGContext
绘制保存图像以在UIImageView中显示。
您需要保持当前的context
直到画线过程结束,之后您需要保留当前图像并清除{{1} },再次绘制保存的图像,然后绘制新行。触摸结束时,请清除当前context
并保存当前图像,然后再次开始新的线条绘制过程。
context