Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var tempImageView : UIImageView!
- var patternImage = UIImage(named: "xxx.png")!
- var brushWidth : CGFloat = 50.0
- var opacity : CGFloat = 1.0
- var lastPoint = CGPoint.zero
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesBegan(touches, with: event)
- if let touch = touches.first {
- lastPoint = touch.location(in: self)
- }
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- super.touchesMoved(touches, with: event)
- if let touch = touches.first {
- let currentPoint = touch.location(in: self)
- drawLine(fromPoint: lastPoint, toPoint: currentPoint)
- lastPoint = currentPoint
- }
- }
- func drawLine(fromPoint:CGPoint, toPoint:CGPoint) {
- UIGraphicsBeginImageContext(self.frame.size)
- let context = UIGraphicsGetCurrentContext()
- tempImageView.image?.draw(in: self.bounds)
- context?.draw(patternImage.cgImage!, in: CGRect(x: fromPoint.x , y: fromPoint.y, width:brushWidth , height:brushWidth))
- tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
- tempImageView.alpha = opacity
- UIGraphicsEndImageContext()
- }
Add Comment
Please, Sign In to add comment