Skip to content

Instantly share code, notes, and snippets.

@fatbobman
Last active April 27, 2024 23:22
Show Gist options
  • Save fatbobman/1ddbe3bd61bbb09c536c9dd2b8eeb739 to your computer and use it in GitHub Desktop.
Save fatbobman/1ddbe3bd61bbb09c536c9dd2b8eeb739 to your computer and use it in GitHub Desktop.
import SwiftUI
import TipKit
struct Tip1: ShowTip {
var title: Text = Text("Step1 Tips")
@Parameter
static var show: Bool = false
var rules: [Rule] {
[
#Rule(Self.$show) {
$0 == true
},
]
}
}
struct Tip2: ShowTip {
var title: Text = Text("Step2 Tips")
@Parameter
static var show: Bool = false
var rules: [Rule] {
[
#Rule(Self.$show) {
$0 == true
},
]
}
}
struct ContentView: View {
private var tip1 = Tip1()
private var tip2 = Tip2()
var body: some View {
List {
Button("Show Tip1") {
Tip1.show = true
}
TipView(tip1, arrowEdge: .bottom)
.tipViewStyle(MyTipStyle(tip: Tip2.self))
Text("Step1")
TipView(tip2, arrowEdge: .bottom)
Text("Step2")
}
.padding()
}
}
protocol ShowTip: Tip {
static var show: Bool { get set }
}
struct MyTipStyle<T: ShowTip>: TipViewStyle {
let tip: T.Type
func makeBody(configuration: Configuration) -> some View {
VStack {
if let image = configuration.image {
image
.font(.title2)
.foregroundStyle(.green)
}
if let title = configuration.title {
title
.bold()
.font(.headline)
.textCase(.uppercase)
}
if let message = configuration.message {
message
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity,alignment:.leading)
.backgroundStyle(.thinMaterial)
.overlay(alignment: .topTrailing) {
Image(systemName: "multiply")
.font(.title2)
.alignmentGuide(.top) { $0[.top] - 5 }
.alignmentGuide(.trailing) { $0[.trailing] + 5 }
.foregroundStyle(.secondary)
.onTapGesture {
// Invalidate Reason
configuration.tip.invalidate(reason: .tipClosed)
tip.show = true
}
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment