├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── Images └── preview.gif ├── LICENSE ├── Package.swift ├── README.md └── Sources └── SwiftUIShakeGesture └── SwiftUIShakeGesture.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Images/preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/globulus/swiftui-shake-gesture/e32b9640f15a68cf2041f0deacd51ab1a4623190/Images/preview.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Gordan Glavaš 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "SwiftUIShakeGesture", 8 | platforms: [ 9 | .iOS(.v13) 10 | ], 11 | products: [ 12 | // Products define the executables and libraries a package produces, and make them visible to other packages. 13 | .library( 14 | name: "SwiftUIShakeGesture", 15 | targets: ["SwiftUIShakeGesture"]), 16 | ], 17 | dependencies: [ 18 | // Dependencies declare other packages that this package depends on. 19 | // .package(url: /* package url */, from: "1.0.0"), 20 | ], 21 | targets: [ 22 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 23 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 24 | .target( 25 | name: "SwiftUIShakeGesture", 26 | dependencies: []), 27 | .testTarget( 28 | name: "SwiftUIShakeGestureTests", 29 | dependencies: ["SwiftUIShakeGesture"]), 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUIShakeGesture 2 | 3 | Simply detect **shake gestures in SwiftUI**. 4 | 5 | ![Preview](https://github.com/globulus/swiftui-shake-gesture/blob/main/Images/preview.gif?raw=true) 6 | 7 | ## Installation 8 | 9 | This component is distributed as a **Swift package**. 10 | 11 | ## Sample usage 12 | 13 | ```swift 14 | struct ShakeTest: View { 15 | @State private var text = "Shake me!" 16 | 17 | var body: some View { 18 | Text(text) 19 | .onShake { // ADD THIS 20 | text = "Shaken at \(Date())" 21 | } 22 | } 23 | } 24 | ``` 25 | 26 | ## Recipe 27 | 28 | Check out [this recipe](https://swiftuirecipes.com/blog/shake-gesture-in-swiftui) for in-depth description of the component and its code. Check out [SwiftUIRecipes.com](https://swiftuirecipes.com) for more **SwiftUI recipes**! 29 | -------------------------------------------------------------------------------- /Sources/SwiftUIShakeGesture/SwiftUIShakeGesture.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public extension Notification.Name { 4 | static let shakeEnded = Notification.Name("ShakeEnded") 5 | } 6 | 7 | public extension UIWindow { 8 | override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { 9 | if motion == .motionShake { 10 | NotificationCenter.default.post(name: .shakeEnded, object: nil) 11 | } 12 | super.motionEnded(motion, with: event) 13 | } 14 | } 15 | 16 | struct ShakeDetector: ViewModifier { 17 | let onShake: () -> Void 18 | 19 | func body(content: Content) -> some View { 20 | content 21 | .onAppear() // this has to be here because of a SwiftUI bug 22 | .onReceive(NotificationCenter.default.publisher(for: .shakeEnded)) { _ in 23 | onShake() 24 | } 25 | } 26 | } 27 | 28 | public extension View { 29 | func onShake(perform action: @escaping () -> Void) -> some View { 30 | self.modifier(ShakeDetector(onShake: action)) 31 | } 32 | } 33 | 34 | struct ShakeTest: View { 35 | @State private var text = "Shake me!" 36 | 37 | var body: some View { 38 | Text(text) 39 | .onShake { 40 | text = "Shaken at \(Date())" 41 | } 42 | } 43 | } 44 | --------------------------------------------------------------------------------