Skip to content

Instantly share code, notes, and snippets.

@zahmedpk
zahmedpk / SwiftUICoordinateSpacesExample.swift
Last active January 7, 2021 10:35
Use Coordinate spaces to convert tap location.
// SwiftUI Coordinate Spaces example
// Xcode 12.3
// We show two colored rectangles on screen. When any of these rectangles is tapped
// we convert the tap location into VStack's coordinate space and show a circle at
// that location.
import SwiftUI
struct ContentView: View {
@State var locationOfTapInStackCoords: CGPoint? {
@zahmedpk
zahmedpk / SwiftUIGestures.swift
Last active January 8, 2021 14:25
Use multiple gestures to interact with an object.
// SwiftUI Gestures Tiny Example
// Xcode 12.3
// This gist shows how to use multiple gestures on same object.
// We draw a rectangle on screen that can be
// 1. pinch zoomed
// 2. rotated
// 3. tapped (single tap) to change opacity
// 4. tapped (double tap) to change color
// 5. long press to scale up (+0.2)
@zahmedpk
zahmedpk / SwiftUIDragNDropText.swift
Last active January 6, 2021 13:11
How to drag and drop text from one view to another view within an app.
// Xcode 12.3
import SwiftUI
class ViewModel: ObservableObject {
@Published var itemsInUpperStack: [String] = ["Drag me down", "Some more text"]
@Published var itemsInLowerStack: [String] = ["Drag me up", "one two three"]
func move(item: String) {
if !itemsInLowerStack.contains(item) {
itemsInLowerStack.append(item)
@zahmedpk
zahmedpk / SwiftUIOffsetTransition.swift
Last active January 6, 2021 13:12
SwiftUI - Making views appear/disappear with an offset transition
// Xcode 12.3
import SwiftUI
struct ContentView: View {
@State private var arr = [1,2,3]
var body: some View {
ForEach(arr, content: {
number in
RoundedRectangle(cornerRadius: 10)
.foregroundColor(.red)