Je veux que l'utilisateur de dessiner un rectangle sur UIImageView
j'ai ajouté deux variables pour les premiers une monotouches dernière J'ai ajouté cette fonction: -Dessiner un rectangle sur UIImageView en utilisant Core Graphics
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()
}
ajouter la méthode (touchMoved
), il attire de nombreux rectangles
ajouter la méthode (touchEnded
) il tire un, mais il ne semble pas lorsque l'utilisateur déplace la touche
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)
}
}
Je veux que l'utilisateur puisse étendre le rectangle lorsque touchMoved
et tirer lorsque le touchEnded
.
où je peux trouver des informations sur les "touches prédictives" vous savez où je peux trouver un tutoriel ou quelque chose? –
@ReinierMelian - Voir WWDC 2015 [Advanced Touch Input sur iOS] (https://developer.apple.com/videos/play/wwdc2015/233/). Ou juste google "didacticiel PredictTouches" et vous verrez beaucoup de liens. – Rob
Merci beaucoup, mais il dessine un rectangle et quand un nouvel événement touchBegan commence un nouveau rectangle et supprime le précédent. Je veux enregistrer un dessin après que le toucher se termine et commencer un nouveau –