Last active
August 10, 2020 06:53
-
-
Save rayfix/400893d383bae7fbab4a69f7b535d5d1 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
// | |
// ContentView.swift | |
// EditDemo | |
// | |
// Created by Ray Fix on 7/25/20. | |
// | |
import SwiftUI | |
// This is a hack! | |
func hideKeyboard() { | |
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) | |
} | |
struct InlineTextEditor: View { | |
@Binding var text: String | |
var isValid: (String) -> Bool | |
@State private var original: String = "" | |
@State private var error = false | |
var body: some View { | |
Text(text) | |
.hidden() | |
.overlay(TextField("", text: $text) { isEditing in | |
original = text | |
error = false | |
} onCommit: { | |
if isValid(text) { | |
error = false | |
} else { | |
withAnimation { | |
error = true | |
} | |
text = original | |
} | |
}).shake(enable: error) | |
} | |
} | |
extension View { | |
func shake(enable: Bool) -> some View { | |
self.modifier(ShakeEffect(enable)) | |
//.animation(enable ? .linear(duration: 0.25) : .none) | |
} | |
} | |
struct ShakeEffect: GeometryEffect { | |
init(_ activated: Bool) { | |
x = activated ? 4 : 0 | |
} | |
func effectValue(size: CGSize) -> ProjectionTransform { | |
ProjectionTransform(CGAffineTransform(translationX: 5*sin(1.2 * .pi * x), y: 0) ) | |
} | |
var x: CGFloat | |
var animatableData: CGFloat { | |
get { x } | |
set { x = newValue } | |
} | |
} | |
struct FolderView: View { | |
@Binding var filename: String | |
var body: some View { | |
VStack { | |
Image(systemName: "folder.fill") | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.foregroundColor(.blue) | |
.frame(width: 70) | |
InlineTextEditor(text: $filename) { input in | |
!input.isEmpty && input.count < 10 | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
@State private var offset = CGSize.zero | |
@State private var start = CGSize.zero | |
@State private var root = "Images" | |
@State private var shaking = false | |
var body: some View { | |
VStack { | |
TextField("", text: $root) | |
Spacer() | |
} | |
} | |
// var body: some View { | |
// | |
// ZStack { | |
// | |
// Color(.systemBackground).onTapGesture { | |
// hideKeyboard() | |
// } | |
// | |
// VStack { | |
// FolderView(filename: $root).shake(enable: shaking) | |
// .offset(offset) | |
// .gesture( | |
// DragGesture(minimumDistance: 1, coordinateSpace: .local) | |
// .onChanged { gesture in | |
// offset = gesture.translation - start | |
// } | |
// .onEnded { _ in | |
// start = -offset | |
// withAnimation { | |
// shaking.toggle() | |
// } | |
// } | |
// ) | |
// } | |
// } | |
// } | |
} | |
func +(lhs: CGSize, rhs: CGSize) -> CGSize { | |
CGSize(width: lhs.width+rhs.width, height: lhs.height+rhs.height) | |
} | |
prefix func -(size: CGSize) -> CGSize { | |
CGSize(width: -size.width, height: -size.height) | |
} | |
func -(lhs: CGSize, rhs: CGSize) -> CGSize { | |
lhs + (-rhs) | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment