1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- //
- // PhotoEditor+Drawing.swift
- // Photo Editor
- //
- // Created by Mohamed Hamed on 6/16/17.
- //
- //
- import UIKit
- extension PhotoEditorViewController {
-
- override public func touchesBegan(_ touches: Set<UITouch>,
- with event: UIEvent?){
- if isDrawing {
- swiped = false
- if let touch = touches.first {
- lastPoint = touch.location(in: self.canvasImageView)
- }
- }
- //Hide stickersVC if clicked outside it
- else if stickersVCIsVisible == true {
- if let touch = touches.first {
- let location = touch.location(in: self.view)
- if !stickersViewController.view.frame.contains(location) {
- removeStickersView()
- }
- }
- }
-
- }
-
- override public func touchesMoved(_ touches: Set<UITouch>,
- with event: UIEvent?){
- if isDrawing {
- // 6
- swiped = true
- if let touch = touches.first {
- let currentPoint = touch.location(in: canvasImageView)
- drawLineFrom(lastPoint, toPoint: currentPoint)
-
- // 7
- lastPoint = currentPoint
- }
- }
- }
-
- override public func touchesEnded(_ touches: Set<UITouch>,
- with event: UIEvent?){
- if isDrawing {
- if !swiped {
- // draw a single point
- drawLineFrom(lastPoint, toPoint: lastPoint)
- }
- }
-
- }
-
- func drawLineFrom(_ fromPoint: CGPoint, toPoint: CGPoint) {
- // 1
- let canvasSize = canvasImageView.frame.integral.size
- UIGraphicsBeginImageContextWithOptions(canvasSize, false, 0)
- if let context = UIGraphicsGetCurrentContext() {
- canvasImageView.image?.draw(in: CGRect(x: 0, y: 0, width: canvasSize.width, height: canvasSize.height))
- // 2
- context.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
- context.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
- // 3
- context.setLineCap( CGLineCap.round)
- context.setLineWidth(5.0)
- context.setStrokeColor(drawColor.cgColor)
- context.setBlendMode( CGBlendMode.normal)
- // 4
- context.strokePath()
- // 5
- canvasImageView.image = UIGraphicsGetImageFromCurrentImageContext()
- }
- UIGraphicsEndImageContext()
- }
-
- }
|