簡體   English   中英

畫直線快4

[英]Draw straight line swift 4

也許是一個簡單的問題。 我正在嘗試制作一個可以繪制直線的應用程序:水平垂直線和對角線。 我也在嘗試對這些線進行測量(還沒有使這個工作..)

我已經修復了我可以畫線但不能畫直線的問題(例如沒有尺子的鉛筆在網上找不到任何文件來畫直線)

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)
        }

    }
}

正如Reinier Melian解釋的那樣,使用核心圖形繪制直線。 觸摸結束后,使用CGContext繪制保存圖像以在UIImageView中顯示。

您需要保留當前context 直到線條繪制過程結束為止 ,之后,您需要保留當前圖像並清除context ,再次繪制保存的圖像,然后繪制新線條。 觸摸結束時,調用“清理當前context並保存當前圖像”,然后再次開始新的線條繪制過程。

import UIKit

class ViewController2: UIViewController {

    @IBOutlet weak var drawingPlace: UIImageView!

    var startTouch : CGPoint?
    var secondTouch : CGPoint?
    var currentContext : CGContext?
    var prevImage : UIImage?


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        startTouch = touch?.location(in: drawingPlace)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches{
            secondTouch = touch.location(in: drawingPlace)

            if(self.currentContext == nil){
                UIGraphicsBeginImageContext(drawingPlace.frame.size)
                self.currentContext = UIGraphicsGetCurrentContext()
            }else{
                self.currentContext?.clear(CGRect(x: 0, y: 0, width: drawingPlace.frame.width, height: drawingPlace.frame.height))
            }

            self.prevImage?.draw(in: self.drawingPlace.bounds)

            let bezier = UIBezierPath()

            bezier.move(to: startTouch!)
            bezier.addLine(to: secondTouch!)
            bezier.close()

            UIColor.blue.set()

            self.currentContext?.setLineWidth(4)
            self.currentContext?.addPath(bezier.cgPath)
            self.currentContext?.strokePath()
            let img2 = self.currentContext?.makeImage()
            drawingPlace.image = UIImage.init(cgImage: img2!)

        }
    }


    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.currentContext = nil
        self.prevImage = self.drawingPlace.image
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM