Created
March 30, 2020 13:16
-
-
Save davidthorn/898d4bcbfac1c60633c04495ccb724b4 to your computer and use it in GitHub Desktop.
Swift UIView Peaked View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SwiftPeakView.swift | |
// | |
// Created by Thorn, David on 30.03.20. | |
// Copyright © 2020 Thorn, David. All rights reserved. | |
// | |
import UIKit | |
protocol CreatePeak { | |
func drawPeak(rect: CGRect, | |
peakWidth: CGFloat, | |
cornerRadius: CGFloat, | |
strokeWidth: CGFloat, | |
strokeColor: UIColor, | |
context: CGContext) | |
} | |
extension CreatePeak { | |
func drawPeak(rect: CGRect, | |
peakWidth: CGFloat, | |
cornerRadius: CGFloat = 20, | |
strokeWidth: CGFloat = 2, | |
strokeColor: UIColor, | |
context: CGContext) { | |
let xLength = rect.size.width - strokeWidth | |
let yLength = rect.size.height - peakWidth - (cornerRadius * 2) - strokeWidth | |
let halfWayWidth = (xLength / 2) - cornerRadius | |
let start = CGPoint(x: rect.origin.x + (strokeWidth / 2), | |
y: rect.origin.y + cornerRadius + (strokeWidth / 2)) | |
context.move(to: start) // start should be top left corner | |
let bottomLeftCornerPre = CGPoint(x: start.x, y: start.y + yLength) | |
context.addLine(to: bottomLeftCornerPre) | |
context.setLineWidth(strokeWidth) | |
let control1 = CGPoint(x: start.x, y: bottomLeftCornerPre.y + cornerRadius) | |
let bottomLeftCornerPost = CGPoint(x: bottomLeftCornerPre.x + cornerRadius, y: bottomLeftCornerPre.y + cornerRadius) | |
context.addQuadCurve(to: bottomLeftCornerPost, control: control1) | |
let leftPeakEdge = CGPoint(x: bottomLeftCornerPost.x + (halfWayWidth - peakWidth), | |
y: bottomLeftCornerPost.y) | |
context.addLine(to: leftPeakEdge) | |
let bottomPeakEdge = CGPoint(x: leftPeakEdge.x + peakWidth, | |
y: leftPeakEdge.y + peakWidth) | |
context.addLine(to: bottomPeakEdge) | |
let rightPeakEdge = CGPoint(x: bottomPeakEdge.x + peakWidth, | |
y: bottomPeakEdge.y - peakWidth) | |
context.addLine(to: rightPeakEdge) | |
let bottomRightCorner = CGPoint(x: rightPeakEdge.x + halfWayWidth - peakWidth, | |
y: rightPeakEdge.y) | |
context.addLine(to: bottomRightCorner) | |
let control2 = CGPoint(x: bottomRightCorner.x + cornerRadius, | |
y: bottomRightCorner.y) | |
let bottomRightCornerPost = CGPoint(x: bottomRightCorner.x + cornerRadius, y: bottomRightCorner.y - cornerRadius) | |
context.addQuadCurve(to: bottomRightCornerPost, control: control2) | |
let topRightCorner = CGPoint(x: bottomRightCornerPost.x, | |
y: start.y) | |
context.addLine(to: topRightCorner) | |
let control3 = CGPoint(x: topRightCorner.x, y: topRightCorner.y - cornerRadius) | |
let topRightCornerPre = CGPoint(x: topRightCorner.x - cornerRadius, | |
y: topRightCorner.y - cornerRadius) | |
context.addQuadCurve(to: topRightCornerPre, control: control3) | |
let topLeftCorner = CGPoint(x: start.x + cornerRadius, | |
y: start.y - cornerRadius) | |
context.addLine(to: topLeftCorner) | |
let control4 = CGPoint(x: topLeftCorner.x - cornerRadius, | |
y: topLeftCorner.y) | |
let topLeftCornerPost = CGPoint(x: start.x, y: start.y) | |
context.addQuadCurve(to: topLeftCornerPost, control: control4) | |
context.setStrokeColor(strokeColor.cgColor) | |
context.strokePath() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment