1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| struct PieceSlicer { enum PathDrawType { case line case outside case inner } enum Direction { case left case right case top case bottom } var leftDraw: PathDrawType = .line var topDraw: PathDrawType = .line var rightDraw: PathDrawType = .line var bottomDraw: PathDrawType = .line
var holeRadius: CGFloat = 6.0 init(left: PathDrawType, top: PathDrawType, right: PathDrawType, bottom: PathDrawType) { leftDraw = left topDraw = top rightDraw = right bottomDraw = bottom } func draw(in rect: CGRect) -> UIBezierPath? { let startX = rect.origin.x let startY = rect.origin.y let width = rect.size.width let height = rect.size.height let path = UIBezierPath() path.move(to: CGPoint(x: startX, y: startY)) path.addLine(to: CGPoint(x: (width + startX) / 2 - holeRadius, y: startY)) if bottomDraw == .outside { path.addArc(withCenter: CGPoint(x: (width+startX) / 2, y: startY - holeRadius), radius: holeRadius, startAngle: CGFloat(180.0).toRadians(), endAngle: CGFloat(0.0).toRadians(), clockwise: true) } else if bottomDraw == .inner { path.addArc(withCenter: CGPoint(x: (width+startX) / 2, y: startY + holeRadius), radius: holeRadius, startAngle: CGFloat(180.0).toRadians(), endAngle: CGFloat(0.0).toRadians(), clockwise: false) } path.addLine(to: CGPoint(x: (width+startX) / 2 + holeRadius, y: startY)) path.addLine(to: CGPoint(x: width, y: startY)) path.addLine(to: CGPoint(x: width, y: height / 2)) if rightDraw == .outside { path.addArc(withCenter: CGPoint(x: width + holeRadius, y: height / 2 + holeRadius), radius: holeRadius, startAngle: CGFloat(270.0).toRadians(), endAngle: CGFloat(90.0).toRadians(), clockwise: true) } else if rightDraw == .inner { path.addArc(withCenter: CGPoint(x: width - holeRadius, y: height / 2 + holeRadius), radius: holeRadius, startAngle: CGFloat(270.0).toRadians(), endAngle: CGFloat(90.0).toRadians(), clockwise: false) } path.addLine(to: CGPoint(x: width, y: height / 2 + holeRadius * 2)) path.addLine(to: CGPoint(x: width, y: height)) path.addLine(to: CGPoint(x: (width+startX) / 2 + holeRadius, y: height)) if topDraw == .outside { path.addArc(withCenter: CGPoint(x: (width+startX) / 2, y: height + holeRadius), radius: holeRadius, startAngle: CGFloat(0.0).toRadians(), endAngle: CGFloat(180.0).toRadians(), clockwise: true) } else if topDraw == .inner { path.addArc(withCenter: CGPoint(x: (width+startX) / 2, y: height - holeRadius), radius: holeRadius, startAngle: CGFloat(0.0).toRadians(), endAngle: CGFloat(180.0).toRadians(), clockwise: false) } path.addLine(to: CGPoint(x: (width+startX) / 2.0 - holeRadius, y: height)) path.addLine(to: CGPoint(x: startX, y: height)) path.addLine(to: CGPoint(x: startX, y: height / 2 + startY)) if leftDraw == .outside { path.addArc(withCenter: CGPoint(x: startX - holeRadius, y: height / 2 + holeRadius), radius: holeRadius, startAngle: CGFloat(90.0).toRadians(), endAngle: CGFloat(270.0).toRadians(), clockwise: true) } else if leftDraw == .inner { path.addArc(withCenter: CGPoint(x: startX + holeRadius, y: height / 2 + holeRadius), radius: holeRadius, startAngle: CGFloat(90.0).toRadians(), endAngle: CGFloat(270.0).toRadians(), clockwise: false) } path.addLine(to: CGPoint(x: startX, y: height / 2)) path.close() path.usesEvenOddFillRule = true return path } }
|