Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 27, 2022
1 parent 4fe8b8b commit b5d8ecb
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 46 deletions.
10 changes: 5 additions & 5 deletions Example/PrismExample/PrismExample/Showcase/Logo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct LogoGalleryView: View {
PrismCanvas(tilt: configuration.tilt) {
LogoPrismView(configuration: $configuration)
}
.scaleEffect(y: 0.78)
.scaleEffect(y: 0.76)
.frame(minHeight: 240)
.offset(y: 20)
}
Expand All @@ -40,7 +40,7 @@ struct LogoDetailView: View {
PrismCanvas(tilt: configuration.tilt) {
LogoPrismView(configuration: $configuration)
}
.scaleEffect(y: 0.78)
.scaleEffect(y: 0.76)
.navigationTitle("Logo")
}
}
Expand All @@ -54,11 +54,11 @@ struct LogoPrismView: View {

var body: some View {
PrismView(configuration: configuration) {
side(from: .topLeading, to: .bottomTrailing, fromColor: Color.yellow.opacity(0.75), toColor: Color.white)
side(from: .topLeading, to: .bottomTrailing, fromColor: Color.pink.opacity(0.75), toColor: Color.white)
} left: {
side(from: .bottomTrailing, to: .topTrailing, fromColor: Color.cyan.opacity(0.75), toColor: Color.white)
side(from: .bottomTrailing, to: .topTrailing, fromColor: Color.green.opacity(0.75), toColor: Color.white)
} right: {
side(from: .bottomTrailing, to: .topLeading, fromColor: Color.green.opacity(0.75), toColor: Color.white)
side(from: .bottomTrailing, to: .topLeading, fromColor: Color.yellow.opacity(0.75), toColor: Color.white)
}
}

Expand Down
14 changes: 0 additions & 14 deletions Sources/Prism.swift

This file was deleted.

17 changes: 15 additions & 2 deletions Sources/PrismCanvas.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

import SwiftUI

/**
The base view for prisms.

This applies a perspective transform to simulate a 3D effect.
*/
public struct PrismCanvas<Content: View>: View {
var tilt: CGFloat
var content: Content
public var tilt: CGFloat
public var content: Content

public init(
tilt: CGFloat,
Expand All @@ -26,7 +31,15 @@ public struct PrismCanvas<Content: View>: View {
}
}


public extension PrismCanvas {
/**
The base view for prisms.

This applies a perspective transform to simulate a 3D effect.

This is a convenience initializer.
*/
init(
configuration: PrismConfiguration,
@ViewBuilder content: () -> Content
Expand Down
16 changes: 16 additions & 0 deletions Sources/PrismConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@

import SwiftUI

/**
Values used for configuring `PrismView`.
*/
public struct PrismConfiguration {
/// How much to tilt the prism by. This must match the `tilt` passed into `PrismCanvas`.
public var tilt = CGFloat(0.25)

/// The width and height of the prism.
public var size = CGSize(width: 100, height: 100)

/// The extrusion of the prism.
public var extrusion = CGFloat(30)

/// The levitation (float) of the prism.
public var levitation = CGFloat(0)

/// The color of the shadow.
public var shadowColor = Color.black

/// The opacity of the shadow. Automatically adjusts as needed based on `levitation`.
public var shadowOpacity = CGFloat(0.25)

/**
Values used for configuring `PrismView`.
*/
public init(
tilt: CGFloat = CGFloat(0.25),
size: CGSize = CGSize(width: 100, height: 100),
Expand Down
34 changes: 19 additions & 15 deletions Sources/PrismEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,74 @@
// Created by A. Zheng (github.com/aheze) on 9/26/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//


import SwiftUI

extension View {
public extension View {
/// Perspective transform for the left side of the extruded prism.
func tiltLeft(tilt: CGFloat) -> some View {
modifier(
PrismLeftEffect(tilt: tilt)
)
}

/// Perspective transform for the right side of the extruded prism.
func tiltRight(tilt: CGFloat) -> some View {
modifier(
PrismRightEffect(tilt: tilt)
)
}

/// Perspective transform for the top side of the extruded prism.
func tiltContent(tilt: CGFloat) -> some View {
modifier(
PrismContentEffect(tilt: tilt)
)
}
}

struct PrismLeftEffect: GeometryEffect {
var tilt: CGFloat
/// Perspective transform for the left side of the extruded prism.
public struct PrismLeftEffect: GeometryEffect {
public var tilt: CGFloat

var animatableData: CGFloat {
public var animatableData: CGFloat {
get { tilt }
set { tilt = newValue }
}

func effectValue(size: CGSize) -> ProjectionTransform {
public func effectValue(size: CGSize) -> ProjectionTransform {
return ProjectionTransform(
CGAffineTransform(a: 1, b: 0, c: tilt, d: 1, tx: 0, ty: 0)
)
}
}

struct PrismRightEffect: GeometryEffect {
var tilt: CGFloat
/// Perspective transform for the right side of the extruded prism.
public struct PrismRightEffect: GeometryEffect {
public var tilt: CGFloat

var animatableData: CGFloat {
public var animatableData: CGFloat {
get { tilt }
set { tilt = newValue }
}

func effectValue(size: CGSize) -> ProjectionTransform {
public func effectValue(size: CGSize) -> ProjectionTransform {
return ProjectionTransform(
CGAffineTransform(a: tilt, b: tilt + (1 - tilt), c: 0, d: 1, tx: 0, ty: 0)
)
}
}

struct PrismContentEffect: GeometryEffect {
var tilt: CGFloat
/// Perspective transform for the top side of the extruded prism.
public struct PrismContentEffect: GeometryEffect {
public var tilt: CGFloat

var animatableData: CGFloat {
public var animatableData: CGFloat {
get { tilt }
set { tilt = newValue }
}

func effectValue(size: CGSize) -> ProjectionTransform {
public func effectValue(size: CGSize) -> ProjectionTransform {
let tx: CGFloat = size.height * tilt / 2
let ty: CGFloat = size.width * tilt / 2

Expand All @@ -83,4 +88,3 @@ struct PrismContentEffect: GeometryEffect {
)
}
}

31 changes: 21 additions & 10 deletions Sources/PrismView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

import SwiftUI

/**
A 3D prism. Must be placed inside a `PrismCanvas.`
*/
public struct PrismView<Content: View, Left: View, Right: View>: View {
var configuration: PrismConfiguration
@ViewBuilder var content: Content
@ViewBuilder var left: Left
@ViewBuilder var right: Right
public var configuration: PrismConfiguration
@ViewBuilder public var content: Content
@ViewBuilder public var left: Left
@ViewBuilder public var right: Right

/**
A 3D prism. Must be placed inside a `PrismCanvas.`
*/
public init(
configuration: PrismConfiguration,
@ViewBuilder content: () -> Content,
Expand All @@ -35,16 +41,16 @@ public struct PrismView<Content: View, Left: View, Right: View>: View {
let extrusionXOffset = sin(topAngle) * configuration.extrusion
let extrusionYOffset = cos(topAngle) * configuration.extrusion

content
content /// The top side of the prism.
.frame(width: configuration.size.width, height: configuration.size.height)
.background(
.background( /// The left side of the prism.
left
.frame(width: configuration.size.width, height: extrusionYOffset)
.tiltLeft(tilt: configuration.tilt)
.offset(y: extrusionYOffset),
alignment: .bottom
)
.background(
.background( /// The left side of the prism.
Color.clear
.frame(width: extrusionYOffset, height: configuration.size.height)
.overlay(
Expand All @@ -56,6 +62,8 @@ public struct PrismView<Content: View, Left: View, Right: View>: View {
.offset(x: extrusionYOffset),
alignment: .trailing
)

/// Apply the background.
.background(
configuration.shadowColor
.shadow(
Expand All @@ -69,15 +77,18 @@ public struct PrismView<Content: View, Left: View, Right: View>: View {
y: levitationYOffset + extrusionYOffset + 10
)
.opacity(configuration.shadowOpacity)
.opacity(CGFloat(1) - (CGFloat(configuration.levitation) / 400))
.opacity(CGFloat(1) - (CGFloat(configuration.levitation) / 400)) /// Adjust the shadow and blue based on the `levitation`value.
.blur(radius: 10 + CGFloat(configuration.levitation) / 12)
)
.offset(x: -levitationXOffset, y: -levitationYOffset) /// z height effect
.offset(x: -extrusionXOffset, y: -extrusionYOffset) /// extrusion effect
.offset(x: -levitationXOffset, y: -levitationYOffset) /// Z height effect.
.offset(x: -extrusionXOffset, y: -extrusionYOffset) /// Extrusion effect.
}
}

public extension PrismView {
/**
A 3D prism. Must be placed inside a `PrismCanvas`. This is a convenience initializer.
*/
init(
tilt: CGFloat,
size: CGSize,
Expand Down
9 changes: 9 additions & 0 deletions Sources/Templates/PrismColorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import SwiftUI

/**
A template for a solid-color prism.
*/
public struct PrismColorView: View {
var configuration: PrismConfiguration
var color: Color

/**
A template for a solid-color prism.
*/
public init(
configuration: PrismConfiguration,
color: Color
Expand All @@ -32,6 +38,9 @@ public struct PrismColorView: View {
}

public extension PrismColorView {
/**
A template for a solid-color prism. This is a convenience initializer.
*/
init(
tilt: CGFloat,
size: CGSize,
Expand Down
9 changes: 9 additions & 0 deletions Sources/Templates/PrismGlassView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

import SwiftUI

/**
A template for a glass-style prism.
*/
public struct PrismGlassView: View {
var configuration: PrismConfiguration
var color: Color
var opacity: CGFloat

/**
A template for a glass-style prism.
*/
public init(
configuration: PrismConfiguration,
color: Color,
Expand All @@ -38,6 +44,9 @@ public struct PrismGlassView: View {
}

public extension PrismGlassView {
/**
A template for a glass-style prism. This is a convenience initializer.
*/
init(
tilt: CGFloat,
size: CGSize,
Expand Down
9 changes: 9 additions & 0 deletions Sources/Templates/PrismGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import SwiftUI

/**
A template for a gradient prism.
*/
public struct PrismGradientView: View {
var configuration: PrismConfiguration
var gradient: Gradient

/**
A template for a gradient prism.
*/
public init(
configuration: PrismConfiguration,
gradient: Gradient
Expand All @@ -34,6 +40,9 @@ public struct PrismGradientView: View {
}

public extension PrismGradientView {
/**
A template for a gradient prism. This is a convenience initializer.
*/
init(
tilt: CGFloat,
size: CGSize,
Expand Down
9 changes: 9 additions & 0 deletions Sources/Templates/PrismImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import SwiftUI

/**
A template for an image-filled prism.
*/
public struct PrismImageView: View {
var configuration: PrismConfiguration
var image: Image

/**
A template for an image-filled prism.
*/
public init(
configuration: PrismConfiguration,
image: Image
Expand Down Expand Up @@ -39,6 +45,9 @@ public struct PrismImageView: View {
}

public extension PrismImageView {
/**
A template for an image-filled prism. This is a convenience initializer.
*/
init(
tilt: CGFloat,
size: CGSize,
Expand Down
2 changes: 2 additions & 0 deletions Sources/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import SwiftUI

public extension UIColor {

/// Return a SwiftUI `Color` from a `UIColor`.
var color: Color {
return Color(self)
}
Expand Down

0 comments on commit b5d8ecb

Please sign in to comment.