Swiftpack.co -  aerobounce/HoverAwareView as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
aerobounce/HoverAwareView
SwiftUI `.onHover` memory leak workaround. NSView wrapper that works just like the modifier.
.package(url: "https://github.com/aerobounce/HoverAwareView.git", from: "1.1.1")

HoverAwareView

Memory leak free replacement for onHover modifier

This repository introduces SwiftUI's onHover modifier substitute View which is a wrapper of NSView.

Why do you need this?

  • As of Swift 5.2 and Xcode 11.6 / 12.0 beta 4 targeting macOS 10.15.6, in certain circumstances, just attatching a onHover modifier triggers memory leak — preventing ObservedObject in a view to be deallocated. It seems like a bug.
  • The issue is disscussed here:

Example

import SwiftUI
import HoverAwareView

struct ContentView: View {

    @State private var opacity: Double = 1.0

    var body: some View {
        ZStack {
            HoverAwareView { (hovering: Bool) in
                withAnimation(Animation.spring()) {
                    self.opacity = hovering ? 0.5 : 1.0
                }
            }

            Color.black.opacity(opacity)
        }
    }
}

Or use View extension:

struct ContentView: View {

    @State private var opacity: Double = 1.0

    var body: some View {
        Color.black.opacity(opacity).onHoverAware { (hovering: Bool) in
            withAnimation(Animation.spring()) {
                self.opacity = hovering ? 0.5 : 1.0
            }
        }
    }
}

License

MIT License

Copyright © 2020 github.com/aerobounce.

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 above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

GitHub

link
Stars: 11
Last commit: 45 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API