Je vais essayer de faire une application de dessin spécial graphiques swift4 et de base, mais j'ai trois questions ou des problèmes dans le code ci-dessous:graphiques rapides et fondamentaux, attraper points et défaire les images
import UIKit
class ViewTest : UIView{
@IBOutlet weak var drawingPlace: UIImageView!
var secondStart : CGPoint? // the second start point
var startTouch : CGPoint? // start point
var secondTouch : CGPoint?
var finalTouch : CGPoint?
var currnt : CGContext?
var secondImage : UIImage?
var images : [UIImage] = [] // image array so i can undo
var arrayOfPoints : [CGPoint] = [] // array of points so i can choese one of them to start touch
@IBAction func undoButton(_ sender: Any) {
if images.count > 0{
images.removeLast()
}
if arrayOfPoints.count > 0 {
if arrayOfPoints.count == 2 {
arrayOfPoints.removeAll()
}else{
arrayOfPoints.removeLast()
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
secondStart = touch?.location(in: drawingPlace)
if startTouch == nil || images.count == 0{
startTouch = touch?.location(in: drawingPlace)
arrayOfPoints.append(startTouch!)
}else{
for point in arrayOfPoints{
if secondStart == point{
startTouch = point
// the point is to small , the user can't catch the point
}else{
//don't stroke the path
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
secondTouch = touch?.location(in: drawingPlace)
if currnt == nil {
UIGraphicsBeginImageContext(drawingPlace.frame.size)
currnt = UIGraphicsGetCurrentContext()
}else{
currnt?.clear(drawingPlace.bounds)
}
images.last?.draw(in: drawingPlace.bounds)
currnt?.beginPath()
currnt?.setStrokeColor(UIColor.black.cgColor)
currnt?.setLineWidth(3)
currnt?.setLineCap(.round)
currnt?.move(to: startTouch!)
currnt?.addLine(to: secondTouch!)
currnt?.strokePath()
let img = currnt?.makeImage()
drawingPlace.image = UIImage.init(cgImage: img!)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
finalTouch = touch?.location(in: drawingPlace)
arrayOfPoints.append(finalTouch!)
print(arrayOfPoints.count)
currnt = nil
secondImage = drawingPlace.image
images.append(secondImage!)
}
}
First One: quand j'appuie sur le bouton undoButton la dernière ligne ne disparaît pas jusqu'à ce que je dessine une autre ligne, j'ai essayé de mettre setNeedsDisplay() func mais il ne l'a pas résolu.
@IBAction func undoButton(_ sender: Any) {
if images.count > 0{
images.removeLast()
}
if arrayOfPoints.count > 0 {
if arrayOfPoints.count == 2 {
arrayOfPoints.removeAll()
}else{
arrayOfPoints.removeLast()
}
}
}
Deuxième Un: j'ai série de points qui recueillent la première touche et la touche finale, si la touche de démarrage ne videz la touche de démarrage est égal à une partie de ce points, le problème est: je veux si le deuxième départ égal, proche, près du point puis le point de départ tactile égal.
Troisième Un: si le deuxième départ pas près d'un point, donc je ne veux pas tirer quoi que ce soit.
for point in arrayOfPoints{
if secondStart == point{
startTouch = point
// the point is to small , the user can't catch the point
}else{
//don't stroke the path
}
toute idée que je serais reconnaissant, merci pour votre temps. mazen.
Salut là - je pense vous pourriez vouloir regarder l'un des nombreux tutoriels en ligne sur ce sujet. Du point de vue de la conception, l'approche que vous avez choisie va vous donner beaucoup de maux de tête. Jetez un oeil, par exemple, à celui-ci: https://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit. J'espère que cela pourra aider. – Sparky
Je souhaite que je peux @sparky mais tout mon projet sur les lignes droites et je ne peux pas trouver une autre façon de dessiner des lignes droites – mazen