Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:46
Show Gist options
  • Save CodeSlicing/152591e44e84ff0c0ed3091d21b0c523 to your computer and use it in GitHub Desktop.
Save CodeSlicing/152591e44e84ff0c0ed3091d21b0c523 to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing quick tips episode on applying effects to views
//
// AddingEffectsToViewsDemoNative.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
struct AddingEffectsToViewsDemoNative: View {
var body: some View {
LikeButton()
.frame(width: 200, height: 200)
.compositingGroup()
.shadow(color: Color(white: 0.3), radius: 5)
}
}
private struct LikeButton: View {
@State private var isLiked = false
var body: some View {
GeometryReader { (geo: GeometryProxy) in
let minDimension = min(geo.size.width, geo.size.height)
Button {
isLiked.toggle()
} label: {
ZStack {
Circle()
.fill(.blue)
.background(Circle()
.fill(.white)
.frame(width: minDimension * 1.1, height: minDimension * 1.1))
Image(systemName: isLiked ? "hand.thumbsup.fill" : "hand.thumbsup")
.resizable()
.frame(width: minDimension * 0.5, height: minDimension * 0.5)
.offset(y: -minDimension * 0.02)
.foregroundColor(.white)
}
}
}
}
}
struct AddingEffectsToViewsDemoNative_Previews: PreviewProvider {
struct AddingEffectsToViewsDemoNative_Harness: View {
var body: some View {
AddingEffectsToViewsDemoNative()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
}
}
static var previews: some View {
AddingEffectsToViewsDemoNative_Harness()
.previewDevice("iPhone 13 Pro Max")
.previewDisplayName("iPhone 13 Pro Max")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment