-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Feature/path #85
Open
nodehi
wants to merge
2
commits into
master
Choose a base branch
from
feature/path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/path #85
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
127 changes: 127 additions & 0 deletions
127
Example/AltSwiftUIExample/ExampleViews/PathExampleView.swift
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,127 @@ | ||
// | ||
// PathExampleView.swift | ||
// AltSwiftUIExample | ||
// | ||
// Created by Nodehi, Jabbar on 2021/06/03. | ||
// Copyright © 2021 Rakuten Travel. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import AltSwiftUI | ||
import UIKit | ||
|
||
struct PathExampleView: View { | ||
var viewStore = ViewValues() | ||
|
||
@State private var toggle = true | ||
|
||
private let correctY: CGFloat = 50 | ||
private let messedUpY: CGFloat = 250 | ||
|
||
private let correctX: CGFloat = 20 | ||
private let messedUpX: CGFloat = 56 | ||
|
||
private var tickHeight: CGFloat = 50 | ||
private var tickWidth: CGFloat = 50 | ||
|
||
@State private var percentage: CGFloat = .zero | ||
|
||
var body: View { | ||
ScrollView { | ||
VStack { | ||
|
||
// MARK: - Tick | ||
Path { path in | ||
path.move(to: CGPoint(x: 0, y: tickHeight/2)) | ||
path.addLine(to: CGPoint(x: tickWidth/2, y: tickHeight)) | ||
path.addLine(to: CGPoint(x: tickWidth, y: 0)) | ||
} | ||
.trim(from: 0, to: percentage) | ||
.stroke(Color.black, style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) | ||
.animation(.easeOut(duration: 2.0)) | ||
.onAppear { | ||
self.percentage = 1.0 | ||
} | ||
.onDisappear { | ||
self.percentage = .zero | ||
} | ||
|
||
// MARK: - Toggle Button | ||
Button("\(toggle ? "Distort" : "Fix") the 📱 with animation") { | ||
withAnimation { self.toggle.toggle() } | ||
} | ||
.padding(.vertical, 30) | ||
|
||
// MARK: - iPhone Shape | ||
ZStack { | ||
Path { path in | ||
path.move(to: CGPoint(x: 0, y: toggle ? correctY : messedUpY)) | ||
path.addQuadCurve(to: CGPoint(x: 50, y: 0), control: CGPoint(x: 0, y: 0)) | ||
path.addLine(to: CGPoint(x: 300, y: 0)) | ||
path.addQuadCurve(to: CGPoint(x: 350, y: toggle ? correctY : messedUpY), control: CGPoint(x: 350, y: 0)) | ||
path.addLine(to: CGPoint(x: 350, y: 600)) | ||
path.addQuadCurve(to: CGPoint(x: 300, y: 650), control: CGPoint(x: 350, y: 650)) | ||
path.addLine(to: CGPoint(x: 50, y: 650)) | ||
path.addQuadCurve(to: CGPoint(x: 0, y: 600), control: CGPoint(x: 0, y: 650)) | ||
path.addLine(to: CGPoint(x: 0, y: toggle ? correctY : messedUpY)) | ||
} | ||
.fill(.gray) | ||
.strokeBorder(.black) | ||
|
||
Path { path in | ||
path.move(to: CGPoint(x: toggle ? correctX : messedUpX, y: toggle ? correctY : messedUpY)) | ||
path.addQuadCurve(to: CGPoint(x: 50, y: 20), control: CGPoint(x: toggle ? correctX : messedUpX, y: 20)) | ||
|
||
path.addLine(to: CGPoint(x: 90, y: 20)) | ||
path.addQuadCurve(to: CGPoint(x: 100, y: 30), control: CGPoint(x: 100, y: 20)) | ||
path.addQuadCurve(to: CGPoint(x: 120, y: correctY), control: CGPoint(x: 100, y: toggle ? correctY : messedUpY)) | ||
path.addLine(to: CGPoint(x: 230, y: toggle ? correctY : messedUpY)) | ||
path.addQuadCurve(to: CGPoint(x: 250, y: 30), control: CGPoint(x: 250, y: toggle ? correctY : messedUpY)) | ||
path.addQuadCurve(to: CGPoint(x: 260, y: 20), control: CGPoint(x: 250, y: 20)) | ||
|
||
path.addLine(to: CGPoint(x: 300, y: 20)) | ||
path.addQuadCurve(to: CGPoint(x: 330, y: toggle ? correctY : messedUpY), control: CGPoint(x: 330, y: 20)) | ||
path.addLine(to: CGPoint(x: 330, y: 600)) | ||
path.addQuadCurve(to: CGPoint(x: 300, y: 630), control: CGPoint(x: 330, y: 630)) | ||
path.addLine(to: CGPoint(x: 50, y: 630)) | ||
path.addQuadCurve(to: CGPoint(x: toggle ? correctX : messedUpX, y: 600), control: CGPoint(x: toggle ? correctX : messedUpX, y: 630)) | ||
path.addLine(to: CGPoint(x: toggle ? correctX : messedUpX, y: toggle ? correctY : messedUpY)) | ||
} | ||
.fill(.secondary) | ||
.strokeBorder(.black) | ||
} | ||
|
||
// MARK: - Text | ||
Text("Text to mix up UI elements and show that Path occupy actual space.") | ||
.foregroundColor(.orange) | ||
.padding(.vertical, 20) | ||
|
||
// MARK: - Nested red Rectangles | ||
shrinkingSquares() | ||
.strokeBorder(.black) | ||
.fill(.red) | ||
} | ||
.padding(.vertical, 20) | ||
} | ||
.edgesIgnoringSafeArea(.bottom) | ||
.frame(maxWidth: .infinity) | ||
} | ||
|
||
private func shrinkingSquares() -> Path { | ||
let size = 400 | ||
let change = 20 | ||
let path = Path() | ||
|
||
for i in stride(from: 1, through: size, by: change) { | ||
let rect = CGRect( | ||
x: CGFloat(i), | ||
y: CGFloat(i), | ||
width: CGFloat(size) - CGFloat(2 * i), | ||
height: CGFloat(size) - CGFloat(2 * i) | ||
) | ||
path.addRect(rect) | ||
} | ||
return path | ||
} | ||
} |
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,153 @@ | ||
// | ||
// Path.swift | ||
// AltSwiftUI | ||
// | ||
// Created by Nodehi, Jabbar on 2021/06/03. | ||
// | ||
|
||
import UIKit | ||
|
||
/// A view that represents a Path shape. | ||
public struct Path: Shape { | ||
public var viewStore = ViewValues() | ||
|
||
public var fillColor = Color.clear | ||
public var strokeBorderColor = Color.clear | ||
public var style = StrokeStyle() | ||
var trimStartFraction: CGFloat = 0 | ||
var trimEndFraction: CGFloat = 1 | ||
|
||
var myPath = CGMutablePath() | ||
|
||
public var body: View { | ||
EmptyView() | ||
} | ||
|
||
public init() {} | ||
|
||
public init(_ callback: @escaping (inout Path) -> Void) { | ||
callback(&self) | ||
} | ||
|
||
public func trim(from startFraction: CGFloat = 0, to endFraction: CGFloat = 1) -> Self { | ||
var path = self | ||
path.trimStartFraction = startFraction | ||
path.trimEndFraction = endFraction | ||
return path | ||
} | ||
|
||
public func createView(context: Context) -> UIView { | ||
let view = AltShapeView().noAutoresizingMask() | ||
view.overrideIntrinsicContentSize = true | ||
view.updateOnLayout = { [weak view] rect in | ||
guard let view = view else { return } | ||
updatePath(view: view, path: UIBezierPath(cgPath: myPath), animation: nil) | ||
} | ||
updateView(view, context: context.withoutTransaction) | ||
return view | ||
} | ||
|
||
public func updateView(_ view: UIView, context: Context) { | ||
guard let view = view as? AltShapeView else { return } | ||
let oldView = view.lastRenderableView?.view as? Path | ||
|
||
let width = context.viewValues?.viewDimensions?.width ?? view.bounds.width | ||
let height = context.viewValues?.viewDimensions?.height ?? view.bounds.height | ||
view.pathBoundingBox = myPath.boundingBox | ||
view.setNeedsLayout() | ||
let animation = context.transaction?.animation | ||
view.lastSizeFromViewUpdate = CGSize(width: width, height: height) | ||
|
||
if fillColor.rawColor != Color.clear.rawColor { | ||
updatePath( | ||
view: view, | ||
path: UIBezierPath(cgPath: myPath), | ||
animation: animation) | ||
} else { | ||
if context.viewValues?.viewDimensions != oldView?.viewStore.viewDimensions { | ||
updatePath(view: view, path: UIBezierPath(cgPath: myPath), animation: animation) | ||
} | ||
performUpdate(layer: view.caShapeLayer, keyPath: "strokeStart", newValue: trimStartFraction, animation: animation, oldValue: oldView?.trimStartFraction) | ||
performUpdate(layer: view.caShapeLayer, keyPath: "strokeEnd", newValue: trimEndFraction, animation: animation, oldValue: oldView?.trimEndFraction) | ||
} | ||
updateShapeLayerValues(view: view, context: context) | ||
} | ||
} | ||
|
||
extension Path { | ||
|
||
public func addRoundedRect( | ||
in rect: CGRect, | ||
cornerWidth: CGFloat, | ||
cornerHeight: CGFloat, | ||
transform: CGAffineTransform = .identity | ||
) { | ||
myPath.addRoundedRect(in: rect, cornerWidth: cornerWidth, cornerHeight: cornerHeight, transform: transform) | ||
} | ||
|
||
public func move(to p: CGPoint) { | ||
myPath.move(to: p) | ||
} | ||
public func addLine(to p: CGPoint) { | ||
myPath.addLine(to: p) | ||
} | ||
|
||
public func addQuadCurve(to end: CGPoint, control: CGPoint, transform: CGAffineTransform = .identity) { | ||
myPath.addQuadCurve(to: end, control: control, transform: transform) | ||
} | ||
|
||
public func addCurve(to end: CGPoint, control1: CGPoint, control2: CGPoint, transform: CGAffineTransform = .identity) { | ||
myPath.addCurve(to: end, control1: control1, control2: control2, transform: transform) | ||
} | ||
|
||
public func addRect(_ rect: CGRect, transform: CGAffineTransform = .identity) { | ||
myPath.addRect(rect, transform: transform) | ||
} | ||
|
||
public func addRects(_ rects: [CGRect], transform: CGAffineTransform = .identity) { | ||
myPath.addRects(rects, transform: transform) | ||
} | ||
|
||
public func addLines(between points: [CGPoint], transform: CGAffineTransform = .identity) { | ||
myPath.addLines(between: points, transform: transform) | ||
} | ||
|
||
public func addEllipse(in rect: CGRect, transform: CGAffineTransform = .identity) { | ||
myPath.addEllipse(in: rect, transform: transform) | ||
} | ||
|
||
public func addRelativeArc( | ||
center: CGPoint, | ||
radius: CGFloat, | ||
startAngle: CGFloat, | ||
delta: CGFloat, | ||
transform: CGAffineTransform = .identity | ||
) { | ||
myPath.addRelativeArc(center: center, radius: radius, startAngle: startAngle, delta: delta, transform: transform) | ||
} | ||
|
||
public func addArc( | ||
center: CGPoint, | ||
radius: CGFloat, | ||
startAngle: CGFloat, | ||
endAngle: CGFloat, | ||
clockwise: Bool, | ||
transform: CGAffineTransform = .identity | ||
) { | ||
myPath.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise, transform: transform) | ||
} | ||
|
||
public func addArc( | ||
tangent1End: CGPoint, | ||
tangent2End: CGPoint, | ||
radius: CGFloat, | ||
transform: CGAffineTransform = .identity | ||
) { | ||
myPath.addArc(tangent1End: tangent1End, tangent2End: tangent2End, radius: radius, transform: transform) | ||
} | ||
|
||
public func addPath(_ path: CGPath, transform: CGAffineTransform = .identity) { | ||
myPath.addPath(path, transform: transform) | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused_closure_parameter
)