├── .gitignore ├── LICENSE.md ├── Package.swift ├── README.md ├── Sources └── SeaTouch │ ├── RippleShapeLayer.swift │ ├── SeaTouch.swift │ ├── TouchCatcher.swift │ └── UIWindow+touches.swift ├── Tests ├── LinuxMain.swift └── SeaTouchTests │ ├── SeaTouchTests.swift │ └── XCTestManifests.swift ├── demo.apng ├── demo.gif └── demo.mp4 /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Michael Redig 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.2 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: "SeaTouch", 8 | platforms: [ 9 | .iOS(.v10), 10 | ], 11 | products: [ 12 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 13 | .library( 14 | name: "SeaTouch", 15 | targets: ["SeaTouch"]), 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 which this package depends on. 24 | .target( 25 | name: "SeaTouch", 26 | dependencies: []), 27 | .testTarget( 28 | name: "SeaTouchTests", 29 | dependencies: ["SeaTouch"]), 30 | ] 31 | ) 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SeaTouch 2 | 3 | Displays where the user is touching on screen with elegant ripples and dots. 4 | 5 | ![preview](demo.apng) 6 | 7 | 8 | ### Installation: 9 | 10 | 1. Add the following to your swift packages in Xcode: 11 | * `https://github.com/mredig/SeaTouch.git` 12 | 1. Early in your app lifecycle (eg, `AppDelegate`, `SceneDelegate`, or even just the first `UIViewController` on screen): 13 | ```swift 14 | //... 15 | import SeaTouch 16 | 17 | //... 18 | func someEarlyCalledMethod() { 19 | //... 20 | 21 | #if DEBUG 22 | window?.showTouches() 23 | #endif 24 | } 25 | ``` 26 | 1. Magic! 27 | -------------------------------------------------------------------------------- /Sources/SeaTouch/RippleShapeLayer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CARippleShapeLayer.swift 3 | // DemoApp 4 | // 5 | // Created by Michael Redig on 6/17/20. 6 | // Copyright © 2020 Red_Egg Productions. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class RippleShapeLayer: CAShapeLayer { 12 | 13 | private var animatingSublayers: Set = [] 14 | private var availableSublayers: Set = [] 15 | 16 | func dequeueRippleSublayer() -> CAShapeLayer { 17 | let layer: CAShapeLayer 18 | if let available = availableSublayers.first { 19 | layer = available 20 | } else { 21 | layer = CAShapeLayer() 22 | layer.fillColor = UIColor.clear.cgColor 23 | layer.strokeColor = UIColor.darkGray.withAlphaComponent(0.8).cgColor 24 | layer.lineWidth = 8 25 | } 26 | 27 | addSublayer(layer) 28 | availableSublayers.remove(layer) 29 | animatingSublayers.insert(layer) 30 | 31 | return layer 32 | } 33 | } 34 | 35 | extension RippleShapeLayer: CAAnimationDelegate { 36 | func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { 37 | for layer in animatingSublayers { 38 | if (layer.animationKeys() ?? []).isEmpty { 39 | layer.removeFromSuperlayer() 40 | availableSublayers.insert(layer) 41 | animatingSublayers.remove(layer) 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/SeaTouch/SeaTouch.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | 4 | class SeaTouch { 5 | static let shared = SeaTouch() 6 | 7 | private var trackedWindows = [UIWindow: (touches: TouchCatcher, layer: RippleShapeLayer)]() 8 | private var trackedLayers = [TouchCatcher: RippleShapeLayer]() 9 | private var trackedCatchers = [TouchCatcher: UIWindow]() 10 | 11 | let radius: CGFloat = 25 12 | private var circleSize: CGSize { 13 | CGSize(width: radius * 2, height: radius * 2) 14 | } 15 | 16 | private init() {} 17 | 18 | /// Initiates touch tracking on a given window, also caching important references to speed up future execution. 19 | func trackTouches(on window: UIWindow) { 20 | guard trackedWindows[window] == nil else { return } 21 | let catcher = TouchCatcher(target: self, action: #selector(touchesUpdated(_:))) 22 | 23 | window.addGestureRecognizer(catcher) 24 | 25 | let shapeLayer = RippleShapeLayer() 26 | shapeLayer.fillColor = UIColor.lightGray.withAlphaComponent(0.6).cgColor 27 | shapeLayer.strokeColor = UIColor.darkGray.withAlphaComponent(0.8).cgColor 28 | // would use .greatestFiniteMagnitude, but CoreAnimation complains. Probably not using Double internally? 29 | shapeLayer.zPosition = CGFloat(Float.greatestFiniteMagnitude) 30 | 31 | window.layer.addSublayer(shapeLayer) 32 | 33 | trackedWindows[window] = (catcher, shapeLayer) 34 | trackedLayers[catcher] = shapeLayer 35 | trackedCatchers[catcher] = window 36 | } 37 | 38 | /// Ceases touch tracking on a given window, releasing previously cached references. 39 | func stopTrackingTouches(on window: UIWindow) { 40 | guard let (catcher, shapeLayer) = trackedWindows[window] else { return } 41 | 42 | window.removeGestureRecognizer(catcher) 43 | shapeLayer.removeFromSuperlayer() 44 | 45 | trackedWindows[window] = nil 46 | trackedLayers[catcher] = nil 47 | trackedCatchers[catcher] = nil 48 | } 49 | 50 | /// This is called by the gesture recognizer when a touch event occurrs. This should not be called directly. 51 | @objc func touchesUpdated(_ sender: TouchCatcher) { 52 | let allTouches = sender.allTouches 53 | 54 | guard let window = trackedCatchers[sender] else { return } 55 | let locations = allTouches.map { $0.location(in: window) } 56 | 57 | let path = locations.reduce(into: CGMutablePath()) { 58 | let tl = CGPoint(x: $1.x - radius, y: $1.y - radius) 59 | $0.move(to: tl) 60 | let rect = CGRect(origin: tl, size: circleSize) 61 | $0.addEllipse(in: rect) 62 | } 63 | 64 | for touch in sender.newTouches { 65 | guard let animLayer = trackedLayers[sender]?.dequeueRippleSublayer() else { continue } 66 | 67 | let path = CGMutablePath() 68 | let tl = CGPoint(x: 0 - radius, y: 0 - radius) 69 | path.move(to: tl) 70 | let rect = CGRect(origin: tl, size: circleSize) 71 | path.addEllipse(in: rect) 72 | animLayer.path = path 73 | 74 | let location = touch.location(in: window) 75 | let locationTransform = CATransform3DMakeTranslation(location.x, location.y, 0) 76 | let scaleTransform = CATransform3DMakeScale(3, 3, 1) 77 | let destinationTransform = CATransform3DConcat(scaleTransform, locationTransform) 78 | 79 | animLayer.transform = locationTransform 80 | 81 | let scaleAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.transform)) 82 | scaleAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut) 83 | scaleAnimation.fromValue = locationTransform 84 | scaleAnimation.toValue = destinationTransform 85 | 86 | animLayer.opacity = 0 87 | let fadeAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity)) 88 | fadeAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut) 89 | fadeAnimation.fromValue = 1 90 | fadeAnimation.toValue = 0 91 | fadeAnimation.isCumulative = true 92 | 93 | let animations = CAAnimationGroup() 94 | animations.animations = [scaleAnimation, fadeAnimation] 95 | animations.duration = 0.6 96 | 97 | animations.delegate = trackedLayers[sender] 98 | animLayer.add(animations, forKey: "fadeAndScale") 99 | } 100 | 101 | 102 | trackedLayers[sender]?.path = path 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Sources/SeaTouch/TouchCatcher.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Michael Redig on 5/17/20. 6 | // 7 | import UIKit 8 | 9 | 10 | /// This recognizer is configured to detect and forward *every* touch, but also passes through to any following recognizers/touch input. 11 | final class TouchCatcher: UIGestureRecognizer { 12 | 13 | /// Keeps track of all touches throughout their lifetime 14 | private(set) var allTouches: Set = [] 15 | 16 | private var _newTouches: Set = [] 17 | private(set) var newTouches: Set { 18 | get { 19 | let touches = _newTouches 20 | _newTouches = [] 21 | return touches 22 | } 23 | set { 24 | _newTouches = newValue 25 | } 26 | } 27 | 28 | override init(target: Any?, action: Selector?) { 29 | super.init(target: target, action: action) 30 | commonInit() 31 | } 32 | 33 | private func commonInit() { 34 | cancelsTouchesInView = false 35 | requiresExclusiveTouchType = false 36 | delegate = self 37 | } 38 | 39 | override func touchesBegan(_ touches: Set, with event: UIEvent) { 40 | super.touchesBegan(touches, with: event) 41 | newTouches = newTouches.union(touches) 42 | state = .began 43 | } 44 | 45 | override func touchesMoved(_ touches: Set, with event: UIEvent) { 46 | super.touchesMoved(touches, with: event) 47 | allTouches = allTouches.union(touches) 48 | state = .changed 49 | } 50 | 51 | override func touchesEnded(_ touches: Set, with event: UIEvent) { 52 | super.touchesEnded(touches, with: event) 53 | allTouches = allTouches.subtracting(touches) 54 | if allTouches.isEmpty { 55 | state = .ended 56 | } else { 57 | state = .changed 58 | } 59 | } 60 | 61 | override func touchesCancelled(_ touches: Set, with event: UIEvent) { 62 | super.touchesCancelled(touches, with: event) 63 | allTouches = allTouches.subtracting(touches) 64 | if allTouches.isEmpty { 65 | state = .cancelled 66 | } else { 67 | state = .changed 68 | } 69 | } 70 | 71 | /// Nothing should prevent this gesture recognizer from activating for as long as it's enabled. 72 | override func canBePrevented(by preventingGestureRecognizer: UIGestureRecognizer) -> Bool { 73 | false 74 | } 75 | } 76 | 77 | extension TouchCatcher: UIGestureRecognizerDelegate { 78 | /// No touch events should be consumed by this gesture recognizer alone. 79 | func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 80 | true 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Sources/SeaTouch/UIWindow+touches.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Michael Redig on 5/17/20. 6 | // 7 | 8 | import UIKit 9 | 10 | extension UIWindow { 11 | /// After running this function, the app's UIWindow layer will draw at the areas of touches at the highest possible layer on screen. 12 | public func showTouches() { 13 | SeaTouch.shared.trackTouches(on: self) 14 | } 15 | 16 | /// After running this function, the app's UIWindow layer will cease drawing at the areas of touches. 17 | public func hideTouches() { 18 | SeaTouch.shared.stopTrackingTouches(on: self) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import SeaTouchTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += SeaTouchTests.allTests() 7 | XCTMain(tests) 8 | -------------------------------------------------------------------------------- /Tests/SeaTouchTests/SeaTouchTests.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import XCTest 3 | @testable import SeaTouch 4 | 5 | final class SeaTouchTests: XCTestCase { 6 | func testWindowExtension() { 7 | let window = UIWindow() 8 | 9 | window.gestureRecognizers?.forEach { 10 | let theType = type(of: $0) 11 | XCTAssertFalse(TouchCatcher.self == theType) 12 | } 13 | 14 | XCTAssertEqual(window.layer.sublayers, nil) 15 | 16 | window.showTouches() 17 | 18 | let hasCatcher = window.gestureRecognizers?.contains(where: { TouchCatcher.self == type(of: $0) }) 19 | XCTAssertTrue(hasCatcher == true) 20 | 21 | let hasShapeLayer = window.layer.sublayers?.contains(where: { RippleShapeLayer.self == type(of: $0) }) 22 | XCTAssertTrue(hasShapeLayer == true) 23 | XCTAssertEqual(window.layer.sublayers?.count, 1) 24 | 25 | window.hideTouches() 26 | 27 | window.gestureRecognizers?.forEach { 28 | let theType = type(of: $0) 29 | XCTAssertFalse(TouchCatcher.self == theType) 30 | } 31 | 32 | XCTAssertEqual(window.layer.sublayers, nil) 33 | } 34 | 35 | static var allTests = [ 36 | ("testWindowExtension", testWindowExtension), 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /Tests/SeaTouchTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(SeaTouchTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /demo.apng: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mredig/SeaTouch/16ad945867ffc74f4af854ff4fe98a30f7128f90/demo.apng -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mredig/SeaTouch/16ad945867ffc74f4af854ff4fe98a30f7128f90/demo.gif -------------------------------------------------------------------------------- /demo.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mredig/SeaTouch/16ad945867ffc74f4af854ff4fe98a30f7128f90/demo.mp4 --------------------------------------------------------------------------------