Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:46
Show Gist options
  • Save CodeSlicing/73f0dfbad6a8e760a87a571441afe742 to your computer and use it in GitHub Desktop.
Save CodeSlicing/73f0dfbad6a8e760a87a571441afe742 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing quick tips episode on applying effects to views
//
// AddingEffectsToViewsDemo.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright © 2021 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUI
struct AddingEffectsToViewsDemo: View {
var body: some View {
LikeButton()
.frame(200)
.compositingGroup()
.shadowColor(.white(0.3), 5)
}
}
private struct LikeButton: View {
@State private var isLiked = false
var body: some View {
GeometryReader { (geo: GeometryProxy) in
let minDimension = geo.minDimension
Button {
isLiked.toggle()
} label: {
ZStack {
Circle()
.fill(.blue)
.background(Circle().fill(.white).frame(minDimension * 1.1))
SFSymbol(isLiked ? .hand_thumbsup_fill : .hand_thumbsup)
.resizedToFit(minDimension * 0.5)
.yOffset(-minDimension * 0.02)
.foregroundColor(.white)
}
}
}
}
}
struct AddingEffectsToViewsDemo_Previews: PreviewProvider {
struct AddingEffectsToViewsDemo_Harness: View {
var body: some View {
AddingEffectsToViewsDemo()
.greedyFrame()
.ignoresSafeArea()
}
}
static var previews: some View {
AddingEffectsToViewsDemo_Harness()
.previewDevice(.iPhone_13_Pro_Max)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment