diff --git a/Sources/SwiftUITooltip/TooltipConfigurations/ArrowOnlyTooltipConfig.swift b/Sources/SwiftUITooltip/TooltipConfigurations/ArrowOnlyTooltipConfig.swift index 5f99579..97c3d70 100644 --- a/Sources/SwiftUITooltip/TooltipConfigurations/ArrowOnlyTooltipConfig.swift +++ b/Sources/SwiftUITooltip/TooltipConfigurations/ArrowOnlyTooltipConfig.swift @@ -40,6 +40,8 @@ public struct ArrowOnlyTooltipConfig: TooltipConfig { public var animationOffset: CGFloat = 10 public var animationTime: Double = 1 + public var transition: AnyTransition = .opacity + public init() {} public init(side: TooltipSide) { diff --git a/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift b/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift index 56bc445..d41106c 100644 --- a/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift +++ b/Sources/SwiftUITooltip/TooltipConfigurations/DefaultTooltipConfig.swift @@ -40,6 +40,8 @@ public struct DefaultTooltipConfig: TooltipConfig { public var animationOffset: CGFloat = 10 public var animationTime: Double = 1 + public var transition: AnyTransition = .opacity + public init() {} public init(side: TooltipSide) { diff --git a/Sources/SwiftUITooltip/TooltipConfigurations/TooltipConfig.swift b/Sources/SwiftUITooltip/TooltipConfigurations/TooltipConfig.swift index 57b885d..5be92cc 100644 --- a/Sources/SwiftUITooltip/TooltipConfigurations/TooltipConfig.swift +++ b/Sources/SwiftUITooltip/TooltipConfigurations/TooltipConfig.swift @@ -39,4 +39,6 @@ public protocol TooltipConfig { var enableAnimation: Bool { get set } var animationOffset: CGFloat { get set } var animationTime: Double { get set } + + var transition: AnyTransition { get set } } diff --git a/Sources/SwiftUITooltip/TooltipModifier.swift b/Sources/SwiftUITooltip/TooltipModifier.swift index ee78139..1a767b0 100644 --- a/Sources/SwiftUITooltip/TooltipModifier.swift +++ b/Sources/SwiftUITooltip/TooltipModifier.swift @@ -9,13 +9,15 @@ import SwiftUI struct TooltipModifier: ViewModifier { // MARK: - Uninitialised properties - + var enabled: Bool var config: TooltipConfig var content: TooltipContent + // MARK: - Initialisers - init(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) { + init(enabled: Bool, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) { + self.enabled = enabled self.config = config self.content = content() } @@ -179,7 +181,6 @@ struct TooltipModifier: ViewModifier { .overlay(self.arrowView) } .offset(x: self.offsetHorizontal(g), y: self.offsetVertical(g)) - .animation(.easeInOut) .onAppear { self.dispatchAnimation() } @@ -190,7 +191,7 @@ struct TooltipModifier: ViewModifier { func body(content: Content) -> some View { content - .overlay(tooltipBody) + .overlay(enabled ? tooltipBody.transition(config.transition) : nil) } } diff --git a/Sources/SwiftUITooltip/TooltipViewExtension.swift b/Sources/SwiftUITooltip/TooltipViewExtension.swift index 8d2d900..2f98c3b 100644 --- a/Sources/SwiftUITooltip/TooltipViewExtension.swift +++ b/Sources/SwiftUITooltip/TooltipViewExtension.swift @@ -8,27 +8,41 @@ import SwiftUI public extension View { - func tooltip(@ViewBuilder content: @escaping () -> TooltipContent) -> some View { + func tooltip(_ enabled: Bool = true, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { let config: TooltipConfig = DefaultTooltipConfig.shared - return modifier(TooltipModifier(config: config, content: content)) + return modifier(TooltipModifier(enabled: enabled, config: config, content: content)) } - func tooltip(config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { - modifier(TooltipModifier(config: config, content: content)) + func tooltip(_ enabled: Bool = true, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + modifier(TooltipModifier(enabled: enabled, config: config, content: content)) } - func tooltip(_ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + func tooltip(_ enabled: Bool = true, side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { var config = DefaultTooltipConfig.shared config.side = side - return modifier(TooltipModifier(config: config, content: content)) + return modifier(TooltipModifier(enabled: enabled, config: config, content: content)) } + func tooltip(_ enabled: Bool = true, side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + var config = config + config.side = side + + return modifier(TooltipModifier(enabled: enabled, config: config, content: content)) + } + + func tooltip(_ side: TooltipSide, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { + var config = DefaultTooltipConfig.shared + config.side = side + + return modifier(TooltipModifier(enabled: true, config: config, content: content)) + } + func tooltip(_ side: TooltipSide, config: TooltipConfig, @ViewBuilder content: @escaping () -> TooltipContent) -> some View { var config = config config.side = side - return modifier(TooltipModifier(config: config, content: content)) + return modifier(TooltipModifier(enabled: true, config: config, content: content)) } }