-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored, added supported platforms in the Package.swift file.
Remade some of the documentation markup to include examples uses for some of the more difficult to use shapes.
- Loading branch information
Kieran Brown
committed
Oct 15, 2020
1 parent
b9cc0cf
commit 3abb30f
Showing
22 changed files
with
383 additions
and
267 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// AdaptiveLine.swift | ||
// | ||
// | ||
// Created by Kieran Brown on 10/15/20. | ||
// | ||
|
||
import SwiftUI | ||
import CGExtender | ||
|
||
public struct AdaptiveLine: Shape { | ||
public var angle: Angle | ||
public var animatableData: Angle { | ||
get { self.angle } | ||
set { self.angle = newValue } | ||
} | ||
var insetAmount: CGFloat = 0 | ||
|
||
|
||
/// # Adaptive Line | ||
/// | ||
/// This shape creates a line centered inside of and constrained by its bounding box. | ||
/// The end points of the line are the points of intersection of an infinitely long angled line and the container rectangle | ||
/// - Parameters: | ||
/// - angle: The angle of the adaptive line with `.zero` pointing in the positive X direction | ||
/// | ||
/// ## Example Usage | ||
/// | ||
/// ``` | ||
/// struct AdaptiveLineExample: View { | ||
/// @State var angle: Double = 0.5 | ||
/// | ||
/// var body: some View { | ||
/// ZStack { | ||
/// Color(white: 0.1).edgesIgnoringSafeArea(.all) | ||
/// VStack { | ||
/// AdaptiveLine(angle: Angle(degrees: angle*360)) | ||
/// .stroke(Color.white, | ||
/// style: StrokeStyle(lineWidth: 30, lineCap: .round)) | ||
/// Slider(value: $angle) | ||
/// } | ||
/// .padding() | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
public init(angle: Angle = .zero) { | ||
self.angle = angle | ||
} | ||
|
||
public func path(in rect: CGRect) -> Path { | ||
let rect = rect.insetBy(dx: insetAmount, dy: insetAmount) | ||
let w = rect.width | ||
let h = rect.height | ||
let big: CGFloat = 5000000 | ||
|
||
let x1 = w/2 + big*CGFloat(cos(self.angle.radians)) | ||
let y1 = h/2 + big*CGFloat(sin(self.angle.radians)) | ||
let x2 = w/2 - big*CGFloat(cos(self.angle.radians)) | ||
let y2 = h/2 - big*CGFloat(sin(self.angle.radians)) | ||
|
||
let points = lineRectIntersection(x1, y1, x2, y2, rect.minX, rect.minY, w, h) | ||
if points.count < 2 { | ||
return Path { p in | ||
p.move(to: CGPoint(x: rect.minX, y: rect.midY)) | ||
p.addLine(to: CGPoint(x: rect.maxX, y: rect.midY)) | ||
} | ||
} | ||
|
||
return Path { p in | ||
p.move(to: points[0]) | ||
p.addLine(to: points[1]) | ||
} | ||
} | ||
} | ||
|
||
extension AdaptiveLine: InsettableShape { | ||
public func inset(by amount: CGFloat) -> some InsettableShape { | ||
var shape = self | ||
shape.insetAmount += amount | ||
return shape | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.