Guest User

Untitled

a guest
Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. var tempImageView : UIImageView!
  2. var patternImage = UIImage(named: "xxx.png")!
  3.  
  4. var brushWidth : CGFloat = 50.0
  5. var opacity : CGFloat = 1.0
  6. var lastPoint = CGPoint.zero
  7.  
  8. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  9.  
  10. super.touchesBegan(touches, with: event)
  11.  
  12. if let touch = touches.first {
  13. lastPoint = touch.location(in: self)
  14. }
  15. }
  16.  
  17. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  18.  
  19. super.touchesMoved(touches, with: event)
  20.  
  21. if let touch = touches.first {
  22. let currentPoint = touch.location(in: self)
  23. drawLine(fromPoint: lastPoint, toPoint: currentPoint)
  24.  
  25. lastPoint = currentPoint
  26. }
  27. }
  28.  
  29. func drawLine(fromPoint:CGPoint, toPoint:CGPoint) {
  30.  
  31. UIGraphicsBeginImageContext(self.frame.size)
  32. let context = UIGraphicsGetCurrentContext()
  33. tempImageView.image?.draw(in: self.bounds)
  34.  
  35. context?.draw(patternImage.cgImage!, in: CGRect(x: fromPoint.x , y: fromPoint.y, width:brushWidth , height:brushWidth))
  36. tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
  37. tempImageView.alpha = opacity
  38. UIGraphicsEndImageContext()
  39. }
Add Comment
Please, Sign In to add comment