Skip to content

Commit

Permalink
Add interaction sample
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 27, 2022
1 parent e92ab8f commit 8e88b23
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Binary file added Assets/Prism.mp4
Binary file not shown.
4 changes: 4 additions & 0 deletions Example/PrismExample/PrismExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
3C35DD4028E28804006B7A47 /* Basic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3F28E28804006B7A47 /* Basic.swift */; };
3C35DD4228E28818006B7A47 /* Logo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD4128E28818006B7A47 /* Logo.swift */; };
3C35DD4428E28827006B7A47 /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD4328E28827006B7A47 /* Models.swift */; };
3C44857628E34E9C005F7B46 /* Interaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C44857528E34E9C005F7B46 /* Interaction.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -49,6 +50,7 @@
3C35DD3F28E28804006B7A47 /* Basic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Basic.swift; sourceTree = "<group>"; };
3C35DD4128E28818006B7A47 /* Logo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logo.swift; sourceTree = "<group>"; };
3C35DD4328E28827006B7A47 /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
3C44857528E34E9C005F7B46 /* Interaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Interaction.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -133,6 +135,7 @@
3C35DD3928E2846D006B7A47 /* Slime.swift */,
3C35DD3D28E28800006B7A47 /* Transform.swift */,
3C35DD4128E28818006B7A47 /* Logo.swift */,
3C44857528E34E9C005F7B46 /* Interaction.swift */,
);
path = Showcase;
sourceTree = "<group>";
Expand Down Expand Up @@ -214,6 +217,7 @@
buildActionMask = 2147483647;
files = (
3C35DD2928E25D53006B7A47 /* Color.swift in Sources */,
3C44857628E34E9C005F7B46 /* Interaction.swift in Sources */,
3C35DD3A28E2846D006B7A47 /* Slime.swift in Sources */,
3C35DD4028E28804006B7A47 /* Basic.swift in Sources */,
3C35DD3528E27FD5006B7A47 /* Image.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions Example/PrismExample/PrismExample/Gallery/GalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum ExampleKind: String {
case slime = "Slime"
case transform = "Transform"
case logo = "Logo"
case interaction = "Interaction"

case color = "Color"
case gradient = "Gradient"
Expand All @@ -38,6 +39,8 @@ struct GalleryView: View {
TransformGalleryView(model: model)

LogoGalleryView(model: model)

InteractionGalleryView(model: model)
} header: {
Text("Showcase")
.foregroundColor(UIColor.secondaryLabel.color)
Expand Down Expand Up @@ -72,6 +75,8 @@ struct GalleryView: View {
TransformDetailView()
case .logo:
LogoDetailView()
case .interaction:
InteractionDetailView()
case .color:
ColorDetailView()
case .gradient:
Expand Down
98 changes: 98 additions & 0 deletions Example/PrismExample/PrismExample/Showcase/Interaction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// Interaction.swift
// PrismExample
//
// Created by A. Zheng (github.com/aheze) on 9/27/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//

import Prism
import SwiftUI

struct InteractionGalleryView: View {
@ObservedObject var model: ViewModel
@State var configuration = PrismConfiguration(
tilt: 1,
size: .init(width: 60, height: 60),
extrusion: 60
)

var body: some View {
GalleryCardView(model: model, kind: .interaction) {
PrismCanvas(tilt: configuration.tilt) {
InteractionPrismView(configuration: configuration)
}
.scaleEffect(y: 0.76)
.allowsHitTesting(false)
.frame(minHeight: 240)
.offset(y: 20)
}
}
}

struct InteractionDetailView: View {
@State var configuration = PrismConfiguration(
tilt: 1,
size: .init(width: 60, height: 60),
extrusion: 60
)

var body: some View {
PrismCanvas(tilt: configuration.tilt) {
InteractionPrismView(configuration: configuration)
}
.scaleEffect(y: 0.76)
.navigationTitle("Interaction")
}
}

class InteractionPrismViewModel: ObservableObject {
@Published var rotateBlue = false
@Published var rotateRed = false
@Published var rotateGreen = false

func rotate(keyPath: ReferenceWritableKeyPath<InteractionPrismViewModel, Bool>) {
withAnimation(.spring()) {
self[keyPath: keyPath] = true
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
withAnimation(.spring()) {
self[keyPath: keyPath] = false
}
}
}
}

struct InteractionPrismView: View {
var configuration: PrismConfiguration
@StateObject var model = InteractionPrismViewModel()

var body: some View {
PrismView(configuration: configuration) {
Button {
model.rotate(keyPath: \.rotateBlue)
} label: {
Color.blue
.rotationEffect(.degrees(model.rotateBlue ? 180 : 0))
}
.buttonStyle(.scaling)
} left: {
Button {
model.rotate(keyPath: \.rotateRed)
} label: {
Color.red
.rotationEffect(.degrees(model.rotateRed ? 180 : 0))
}
.buttonStyle(.scaling)
} right: {
Button {
model.rotate(keyPath: \.rotateGreen)
} label: {
Color.green
.rotationEffect(.degrees(model.rotateGreen ? 180 : 0))
}
.buttonStyle(.scaling)
}
}
}

0 comments on commit 8e88b23

Please sign in to comment.