Created
July 5, 2020 04:43
-
-
Save CastIrony/3aa9a42a18bb64fd5eb86ada399e1fd4 to your computer and use it in GitHub Desktop.
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
import SwiftUI | |
struct ContentView: View | |
{ | |
@State var angle: CGFloat = 0 | |
let animation = Animation.linear(duration: 10).repeatForever(autoreverses: false) | |
var body: some View | |
{ | |
ZStack | |
{ | |
Color(white: 0.9).edgesIgnoringSafeArea(.all) | |
RoundedRectangle(cornerRadius: 25.0, style: .continuous) | |
.fill(Color.white) | |
.overlay(VStack(spacing: 15) | |
{ | |
Text("Hello World!").font(.system(.largeTitle)) | |
Text("Foo").font(.system(.headline)) | |
Text("Bar").font(.system(.caption)) | |
}) | |
.frame(width: 200, height: 200, alignment: .center) | |
.panYawEffect(angle: angle) | |
.angleShadow(angle: angle) | |
.onAppear | |
{ | |
withAnimation(animation) | |
{ | |
angle = CGFloat.pi * 2 | |
} | |
} | |
} | |
} | |
} | |
struct PanYawEffect: GeometryEffect | |
{ | |
var animatableData: CGFloat | |
{ | |
get { angle } | |
set { angle = newValue } | |
} | |
var angle: CGFloat | |
func effectValue(size: CGSize) -> ProjectionTransform | |
{ | |
var transform3d = CATransform3DIdentity; | |
transform3d.m34 = -1 / max(size.width, size.height) | |
transform3d = CATransform3DTranslate(transform3d, 0.5 * size.width, 0.5 * size.height, 0) | |
transform3d = CATransform3DRotate(transform3d, sin(angle) * 0.1, 1, 0, 0) | |
transform3d = CATransform3DRotate(transform3d, -cos(angle) * 0.1, 0, 1, 0) | |
transform3d = CATransform3DTranslate(transform3d, -0.5 * size.width, -0.5 * size.height, 0) | |
return ProjectionTransform(transform3d) | |
} | |
} | |
struct AngleShadow : AnimatableModifier | |
{ | |
var angle: CGFloat = 0 | |
var animatableData: CGFloat | |
{ | |
get { angle } | |
set { angle = newValue } | |
} | |
func body(content: Content) -> some View | |
{ | |
content.shadow(radius: 30, x: cos(angle) * 30, y: sin(angle) * 30) | |
} | |
} | |
public extension View | |
{ | |
func angleShadow(angle: CGFloat) -> some View | |
{ | |
return modifier(AngleShadow(angle: angle)) | |
} | |
func panYawEffect(angle: CGFloat) -> some View | |
{ | |
return modifier(PanYawEffect(angle: angle)) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider | |
{ | |
static var previews: some View | |
{ | |
Group | |
{ | |
ContentView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment