|
| 1 | +// |
| 2 | +// QrBorderAroundView.swift |
| 3 | +// QRReader |
| 4 | +// |
| 5 | +// Created by KiraSoga on 2022/08/19. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +class QrBorderAroundView: UIView { |
| 11 | + |
| 12 | + override func layoutSublayers(of layer: CALayer) { |
| 13 | + super.layoutSublayers(of: layer) |
| 14 | + |
| 15 | + let rect = self.bounds |
| 16 | + let cornerRadius: CGFloat = 10 |
| 17 | + |
| 18 | + let upperLeft = self.getQrBorderLayer() |
| 19 | + upperLeft.mask = self.getBorderMaskLayer(.init(x: cornerRadius, |
| 20 | + y: cornerRadius)) |
| 21 | + self.layer.addSublayer(upperLeft) |
| 22 | + |
| 23 | + let upperRight = self.getQrBorderLayer() |
| 24 | + upperRight.mask = self.getBorderMaskLayer(.init(x: rect.maxX - cornerRadius, |
| 25 | + y: cornerRadius)) |
| 26 | + self.layer.addSublayer(upperRight) |
| 27 | + |
| 28 | + let bottomRight = self.getQrBorderLayer() |
| 29 | + bottomRight.mask = self.getBorderMaskLayer(.init(x: rect.maxX - cornerRadius, |
| 30 | + y: rect.maxY - cornerRadius)) |
| 31 | + self.layer.addSublayer(bottomRight) |
| 32 | + |
| 33 | + let bottomLeft = self.getQrBorderLayer() |
| 34 | + bottomLeft.mask = self.getBorderMaskLayer(.init(x: cornerRadius, |
| 35 | + y: rect.maxY - cornerRadius)) |
| 36 | + self.layer.addSublayer(bottomLeft) |
| 37 | + |
| 38 | + self.layer.cornerRadius = 10 |
| 39 | + } |
| 40 | + |
| 41 | + private func getQrBorderLayer() -> CAShapeLayer { |
| 42 | + let rect = self.bounds |
| 43 | + let cornerRadius: CGFloat = 10 |
| 44 | + |
| 45 | + let path = UIBezierPath() |
| 46 | + |
| 47 | + path.move(to: CGPoint(x: 0, y: rect.maxY - cornerRadius)) |
| 48 | + path.addArc(withCenter: CGPoint(x: cornerRadius, y: cornerRadius), |
| 49 | + radius: cornerRadius, |
| 50 | + startAngle: CGFloat(Double.pi), |
| 51 | + endAngle: -CGFloat(Double.pi / 2), |
| 52 | + clockwise: true) |
| 53 | + path.addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, y: cornerRadius), |
| 54 | + radius: cornerRadius, |
| 55 | + startAngle: -CGFloat(Double.pi / 2), |
| 56 | + endAngle: 0, |
| 57 | + clockwise: true) |
| 58 | + path.addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, y: rect.maxY - cornerRadius), |
| 59 | + radius: cornerRadius, |
| 60 | + startAngle: 0, |
| 61 | + endAngle: CGFloat(Double.pi / 2), |
| 62 | + clockwise: true) |
| 63 | + path.addArc(withCenter: CGPoint(x: cornerRadius, y: rect.maxY - cornerRadius), |
| 64 | + radius: cornerRadius, |
| 65 | + startAngle: CGFloat(Double.pi / 2), |
| 66 | + endAngle: CGFloat(Double.pi), |
| 67 | + clockwise: true) |
| 68 | + |
| 69 | + let shapeLayer = CAShapeLayer() |
| 70 | + shapeLayer.fillColor = UIColor.clear.cgColor |
| 71 | + shapeLayer.strokeColor = UIColor.white.cgColor |
| 72 | + shapeLayer.lineWidth = 10 |
| 73 | + shapeLayer.lineCap = .round |
| 74 | + shapeLayer.path = path.cgPath |
| 75 | + |
| 76 | + return shapeLayer |
| 77 | + } |
| 78 | + |
| 79 | + private func getBorderMaskLayer(_ cgPoint: CGPoint) -> CAShapeLayer { |
| 80 | + let path = UIBezierPath() |
| 81 | + |
| 82 | + path.addArc(withCenter: cgPoint, |
| 83 | + radius: 30, |
| 84 | + startAngle: 0, |
| 85 | + endAngle: Double.pi * 2, |
| 86 | + clockwise: true) |
| 87 | + |
| 88 | + let shapeLayer = CAShapeLayer() |
| 89 | + shapeLayer.fillColor = UIColor.black.cgColor |
| 90 | + shapeLayer.path = path.cgPath |
| 91 | + shapeLayer.lineWidth = 10 |
| 92 | + |
| 93 | + return shapeLayer |
| 94 | + } |
| 95 | +} |
0 commit comments