Skip to content

Instantly share code, notes, and snippets.

@Coder-ACJHP
Created December 17, 2018 18:39
Show Gist options
  • Save Coder-ACJHP/7bfff87182c446e97f663b02ba073a4d to your computer and use it in GitHub Desktop.
Save Coder-ACJHP/7bfff87182c446e97f663b02ba073a4d to your computer and use it in GitHub Desktop.
Drawing frame corners and dashed border on view, and drag, scale, rotate the view with gesture recognizers.
//
// SecondViewController.swift
// DrawLines
//
// Created by Onur Işık on 17.12.2018.
// Copyright © 2018 Onur Işık. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var rectBorderedView: UIView!
lazy var panGesture: UIPanGestureRecognizer = {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDragging(_:)))
return panGesture
}()
lazy var rotateGesture: UIRotationGestureRecognizer = {
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))
return rotateGesture
}()
lazy var pinchGesture: UIPinchGestureRecognizer = {
let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handleScale(_:)))
return pinchGestureRecognizer
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .black
self.rectBorderedView.backgroundColor = .clear
self.addDashedBorder(for: self.rectBorderedView)
self.drawCorners(for: self.rectBorderedView, lineWidth: 5, color: .white)
self.addGestures()
}
fileprivate func addGestures() {
panGesture.delegate = self
pinchGesture.delegate = self
rotateGesture.delegate = self
self.rectBorderedView.isUserInteractionEnabled = true
self.rectBorderedView.addGestureRecognizer(panGesture)
self.rectBorderedView.addGestureRecognizer(pinchGesture)
self.rectBorderedView.addGestureRecognizer(rotateGesture)
}
@objc fileprivate func handleDragging(_ gestureRecognizer: UIPanGestureRecognizer) {
guard let currentShape = gestureRecognizer.view else { return }
self.view.bringSubviewToFront(currentShape)
let translation = gestureRecognizer.translation(in: currentShape.superview)
let centerPoint = CGPoint(x: currentShape.center.x + translation.x, y: currentShape.center.y + translation.y)
currentShape.center = centerPoint
gestureRecognizer.setTranslation(.zero, in: currentShape)
}
@objc fileprivate func handleRotate(_ gestureRecognizer: UIRotationGestureRecognizer) {
guard let currentShape = gestureRecognizer.view else { return }
self.view.bringSubviewToFront(currentShape)
currentShape.transform = currentShape.transform.rotated(by: gestureRecognizer.rotation)
gestureRecognizer.rotation = 0.0
}
@objc fileprivate func handleScale(_ gestureRecognizer: UIPinchGestureRecognizer) {
guard let currentShape = gestureRecognizer.view else { return }
self.view.bringSubviewToFront(currentShape)
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
currentShape.transform = (currentShape.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale))
gestureRecognizer.scale = 1.0
}
}
fileprivate func addDashedBorder(for view: UIView) {
let borderFrame: CGRect = view.frame
let shapeLayer = CAShapeLayer()
shapeLayer.name = "borderLayer"
shapeLayer.bounds = borderFrame
shapeLayer.lineJoin = .miter
shapeLayer.lineCap = .square
shapeLayer.lineWidth = 2
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.lineDashPattern = [6, 3]
shapeLayer.frame = borderFrame
shapeLayer.fillColor = UIColor.clear.cgColor
let path = UIBezierPath(rect: view.bounds)
shapeLayer.path = path.cgPath
view.layer.addSublayer(shapeLayer)
}
fileprivate func drawCorners(for view: UIView, lineWidth: CGFloat, color: UIColor) {
let cornerLengthToShow = view.bounds.size.height * 0.05
print(cornerLengthToShow)
// Create Paths Using BeizerPath for all four corners
let topLeftCorner = UIBezierPath()
topLeftCorner.move(to: CGPoint(x: view.bounds.minX, y: view.bounds.minY + cornerLengthToShow))
topLeftCorner.addLine(to: CGPoint(x: view.bounds.minX, y: view.bounds.minY))
topLeftCorner.addLine(to: CGPoint(x: view.bounds.minX + cornerLengthToShow, y: view.bounds.minY))
let topRightCorner = UIBezierPath()
topRightCorner.move(to: CGPoint(x: view.bounds.maxX - cornerLengthToShow, y: view.bounds.minY))
topRightCorner.addLine(to: CGPoint(x: view.bounds.maxX, y: view.bounds.minY))
topRightCorner.addLine(to: CGPoint(x: view.bounds.maxX, y: view.bounds.minY + cornerLengthToShow))
let bottomRightCorner = UIBezierPath()
bottomRightCorner.move(to: CGPoint(x: view.bounds.maxX, y: view.bounds.maxY - cornerLengthToShow))
bottomRightCorner.addLine(to: CGPoint(x: view.bounds.maxX, y: view.bounds.maxY))
bottomRightCorner.addLine(to: CGPoint(x: view.bounds.maxX - cornerLengthToShow, y: view.bounds.maxY ))
let bottomLeftCorner = UIBezierPath()
bottomLeftCorner.move(to: CGPoint(x: view.bounds.minX, y: view.bounds.maxY - cornerLengthToShow))
bottomLeftCorner.addLine(to: CGPoint(x: view.bounds.minX, y: view.bounds.maxY))
bottomLeftCorner.addLine(to: CGPoint(x: view.bounds.minX + cornerLengthToShow, y: view.bounds.maxY))
let combinedPath = CGMutablePath()
combinedPath.addPath(topLeftCorner.cgPath)
combinedPath.addPath(topRightCorner.cgPath)
combinedPath.addPath(bottomRightCorner.cgPath)
combinedPath.addPath(bottomLeftCorner.cgPath)
let shapeLayer = CAShapeLayer()
shapeLayer.path = combinedPath
shapeLayer.strokeColor = color.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = lineWidth
view.layer.addSublayer(shapeLayer)
}
}
extension SecondViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment