From b0a8c3166a243bbf44cf134485535ef57afd3c7c Mon Sep 17 00:00:00 2001 From: Jacob Hearst Date: Wed, 17 Aug 2022 20:43:17 -0500 Subject: [PATCH] Add swipe to dismiss fullscreen image view --- MTGWizard.xcodeproj/project.pbxproj | 4 ++++ .../Common/Extensions/CGPoint+Distance.swift | 15 +++++++++++++++ .../Components/FullScreenImageView.swift | 19 +++++++++++++++++++ MTGWizard/ImageLoader.swift | 9 --------- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 MTGWizard/Common/Extensions/CGPoint+Distance.swift delete mode 100644 MTGWizard/ImageLoader.swift diff --git a/MTGWizard.xcodeproj/project.pbxproj b/MTGWizard.xcodeproj/project.pbxproj index 1c7b075..1c62565 100644 --- a/MTGWizard.xcodeproj/project.pbxproj +++ b/MTGWizard.xcodeproj/project.pbxproj @@ -57,6 +57,7 @@ 2A94C9BE287C79E700E25849 /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A94C9BD287C79E700E25849 /* SearchView.swift */; }; 2A9D3FC1288344DE005228CA /* SettingsTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A9D3FC0288344DE005228CA /* SettingsTab.swift */; }; 2A9E045028A9EE0400B81AAF /* RulesLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A9E044F28A9EE0400B81AAF /* RulesLoader.swift */; }; + 2A9FE48C28ADD03F00769DAA /* CGPoint+Distance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A9FE48B28ADD03F00769DAA /* CGPoint+Distance.swift */; }; 2AA021EE2882419600DDE976 /* ScryfallKit in Frameworks */ = {isa = PBXBuildFile; productRef = 2AA021ED2882419600DDE976 /* ScryfallKit */; }; 2AAF8231289063040061243C /* SearchPersistable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AAF8230289063030061243C /* SearchPersistable.swift */; }; 2AF0FA7E2883567B00769964 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AF0FA7D2883567B00769964 /* Router.swift */; }; @@ -138,6 +139,7 @@ 2A94C9BD287C79E700E25849 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = ""; }; 2A9D3FC0288344DE005228CA /* SettingsTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsTab.swift; sourceTree = ""; }; 2A9E044F28A9EE0400B81AAF /* RulesLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RulesLoader.swift; sourceTree = ""; }; + 2A9FE48B28ADD03F00769DAA /* CGPoint+Distance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGPoint+Distance.swift"; sourceTree = ""; }; 2AAF8230289063030061243C /* SearchPersistable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchPersistable.swift; sourceTree = ""; }; 2AF0FA7D2883567B00769964 /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = ""; }; 2AF0FA862883729D00769964 /* CardPrintingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardPrintingView.swift; sourceTree = ""; }; @@ -289,6 +291,7 @@ 2A2C898F2887153100C428FB /* StringRepresentable.swift */, 2A70406D28A03CDE00E8CE7E /* ObjectList+totalPages.swift */, 2A70406F28A043EE00E8CE7E /* Color+systemBackground.swift */, + 2A9FE48B28ADD03F00769DAA /* CGPoint+Distance.swift */, ); path = Extensions; sourceTree = ""; @@ -479,6 +482,7 @@ 2A9D3FC1288344DE005228CA /* SettingsTab.swift in Sources */, 062A5D73265F1802009FACCB /* LandingView.swift in Sources */, 06F77C0626E7F6D5005C35C0 /* CardDescription.swift in Sources */, + 2A9FE48C28ADD03F00769DAA /* CGPoint+Distance.swift in Sources */, 06F77C1F26E84841005C35C0 /* ManaCostView.swift in Sources */, 2AF0FA7E2883567B00769964 /* Router.swift in Sources */, 2AAF8231289063040061243C /* SearchPersistable.swift in Sources */, diff --git a/MTGWizard/Common/Extensions/CGPoint+Distance.swift b/MTGWizard/Common/Extensions/CGPoint+Distance.swift new file mode 100644 index 0000000..d50d43e --- /dev/null +++ b/MTGWizard/Common/Extensions/CGPoint+Distance.swift @@ -0,0 +1,15 @@ +// +// CGPoint+Distance.swift +// MTGWizard +// +// Created by Jacob Hearst on 8/17/22. +// + +import Foundation + +extension CGPoint { + // sqrt is slow + func distanceSquared(to: CGPoint) -> CGFloat { + return (x - to.x) * (x - to.x) + (y - to.y) * (y - to.y) + } +} diff --git a/MTGWizard/Components/FullScreenImageView.swift b/MTGWizard/Components/FullScreenImageView.swift index 77c942e..6b71540 100644 --- a/MTGWizard/Components/FullScreenImageView.swift +++ b/MTGWizard/Components/FullScreenImageView.swift @@ -10,6 +10,7 @@ import ScryfallKit struct FullScreenImageView: View { @Environment(\.dismiss) var dismiss + @State private var offset: CGSize = .zero var card: Card var viewingSecondFace = false @@ -27,5 +28,23 @@ struct FullScreenImageView: View { .padding() } } + .offset(offset) + .gesture(drag) + } + + var drag: some Gesture { + DragGesture() + .onChanged { value in + offset = value.translation + } + .onEnded { value in + if value.startLocation.distanceSquared(to: value.location) > 110*110 { + dismiss() + } else { + withAnimation { + offset = .zero + } + } + } } } diff --git a/MTGWizard/ImageLoader.swift b/MTGWizard/ImageLoader.swift deleted file mode 100644 index 3f9659a..0000000 --- a/MTGWizard/ImageLoader.swift +++ /dev/null @@ -1,9 +0,0 @@ -// -// ImageLoader.swift -// MTGWizard -// -// Created by Jacob Hearst on 6/21/21. -// - -import SwiftUI -