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
// Introducing a 'combine' function for applying a value to | |
// any function or closure: | |
func combine<A, B>( | |
_ value: A, | |
with closure: @escaping (A) -> B | |
) -> () -> B { | |
return { closure(value) } | |
} | |
// Example |
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
let data: Data = ... | |
let decoder = JSONDecoder() | |
// This will create a Result<Model, Error> instance, which will contain | |
// 'success' if its expression succeeded, and 'failure' if it ended | |
// up throwing an error: | |
let result = Result { | |
try decoder.decode(Model.self, from: data) | |
} |
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
struct DropDown: View { | |
@State var expand = false | |
var body: some View { | |
VStack(alignment: .leading, spacing: 18, content: { | |
HStack { | |
Text("Expand").fontWeight(.heavy).foregroundColor(.white) | |
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13, height: 6).foregroundColor(.white) | |
} | |
.onTapGesture { |
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
struct Container: View { | |
init() { | |
// To remove only extra separators below the list: | |
UITableView.appearance().tableFooterView = UIView() | |
// To remove all separators including the actual ones: | |
UITableView.appearance().separatorStyle = .none | |
} | |
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
struct DismissingKeyboard: ViewModifier { | |
func body(content: Content) -> some View { | |
content | |
.onTapGesture { | |
let keyWindow = UIApplication.shared.connectedScenes | |
.filter({$0.activationState == .foregroundActive}) | |
.map({$0 as? UIWindowScene}) | |
.compactMap({$0}) | |
.first?.windows | |
.filter({$0.isKeyWindow}).first |
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 | |
import simd | |
/// Data structure used to store CGPath commands for easier manipulation of individual components | |
struct PathCommand { | |
let type: CGPathElementType | |
let point: CGPoint | |
let controlPoints: [CGPoint] | |
} |
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 DayView: View { | |
@GestureState var dragState = DragState.inactive | |
@State var viewDragState = CGSize(width: 0, height: 120) | |
var translationOffset: CGSize { | |
return CGSize(width: 0, height: viewDragState.height + dragState.translation.height) | |
} |
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 { | |
// 1. | |
@State private var currentPosition: CGSize = .zero | |
@State private var newPosition: CGSize = .zero | |
var body: some View { | |
// 2. | |
Circle() |
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
// | |
// RelativeOffsetDemo.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, |
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
extension CGSize { | |
public static func + (lhs: CGSize, rhs: CGSize) -> CGSize { | |
CGSize(width: lhs.width+rhs.width, | |
height: lhs.height+rhs.height) | |
} | |
public static func += (lhs: inout CGSize, rhs: CGSize) { | |
lhs.width += rhs.width | |
lhs.height += rhs.height | |
} |
OlderNewer