From 6e4170ef7cafd04b3d8ab8dc5b0469036b2bcd6a Mon Sep 17 00:00:00 2001 From: fermoya Date: Fri, 8 Apr 2022 15:28:42 +0100 Subject: [PATCH] Modifiers to enable/disable digital Crown rotation --- Sources/SwiftUIPager/Pager+Buildable.swift | 20 +++++++++++++++++++ Sources/SwiftUIPager/Pager.swift | 11 +++++++--- .../SwiftUIPager/PagerContent+Buildable.swift | 11 ++++++++++ Sources/SwiftUIPager/PagerContent.swift | 5 ++++- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUIPager/Pager+Buildable.swift b/Sources/SwiftUIPager/Pager+Buildable.swift index 5348849..2d1c676 100644 --- a/Sources/SwiftUIPager/Pager+Buildable.swift +++ b/Sources/SwiftUIPager/Pager+Buildable.swift @@ -173,6 +173,26 @@ extension Pager: Buildable { mutating(keyPath: \.onDigitalCrownRotated, value: callback) } + /// Disables any interaction with the digital crown + @available(iOS, unavailable) + @available(macOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS 7.0, *) + public func disableDigitalCrownRotation() -> Self { + mutating(keyPath: \.allowsDigitalCrownRotation, value: false) + } + + /// Sets whether the `Pager` interacts with the _watchOS_ digital crown + /// + /// - Parameter value: `true` if allowed, `false`, otherwise. Defaults to `true` + @available(iOS, unavailable) + @available(macOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS 7.0, *) + public func allowsDigitalCrownRotation(_ value: Bool = true) -> Self { + mutating(keyPath: \.allowsDigitalCrownRotation, value: value) + } + /// Sets the explicit animation to be used. Defaults to `.standard` /// /// - Parameter value: animation to use while dragging and to page diff --git a/Sources/SwiftUIPager/Pager.swift b/Sources/SwiftUIPager/Pager.swift index 850dbe1..3a16883 100644 --- a/Sources/SwiftUIPager/Pager.swift +++ b/Sources/SwiftUIPager/Pager.swift @@ -111,6 +111,9 @@ public struct Pager: View where PageView: View, Element: /// `true` if `Pager` can be dragged var allowsDragging: Bool = true + /// `true` if `Pager`interacts with the digital crown + var allowsDigitalCrownRotation: Bool = true + /// `true` if pages should have a 3D rotation effect var shouldRotate: Bool = false @@ -153,8 +156,8 @@ public struct Pager: View where PageView: View, Element: /// Callback invoked when a new page is set var onPageChanged: ((Int) -> Void)? - /// Callback for a dragging began event - var onDraggingBegan: (() -> Void)? + /// Callback for a dragging began event + var onDraggingBegan: (() -> Void)? /// Callback for a dragging changed event var onDraggingChanged: ((Double) -> Void)? @@ -232,7 +235,9 @@ public struct Pager: View where PageView: View, Element: #if os(watchOS) if #available(watchOS 7.0, *) { - pagerContent = pagerContent.onDigitalCrownRotated(onDigitalCrownRotated) + pagerContent = pagerContent + .onDigitalCrownRotated(onDigitalCrownRotated) + .allowsDigitalCrownRotation(allowsDigitalCrownRotation) } #endif diff --git a/Sources/SwiftUIPager/PagerContent+Buildable.swift b/Sources/SwiftUIPager/PagerContent+Buildable.swift index 57e9565..dd19efa 100644 --- a/Sources/SwiftUIPager/PagerContent+Buildable.swift +++ b/Sources/SwiftUIPager/PagerContent+Buildable.swift @@ -170,6 +170,17 @@ extension Pager.PagerContent: Buildable { mutating(keyPath: \.onDigitalCrownRotated, value: callback) } + /// Sets whether the `Pager` interacts with the _watchOS_ digital crown + /// + /// - Parameter value: `true` if allowed, `false`, otherwise. Defaults to `true` + @available(iOS, unavailable) + @available(macOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS 7.0, *) + public func allowsDigitalCrownRotation(_ value: Bool = true) -> Self { + mutating(keyPath: \.allowsDigitalCrownRotation, value: value) + } + /// Sets the explicit animation to be used. Defaults to `.standard` /// /// - Parameter value: animation to use while dragging and to page diff --git a/Sources/SwiftUIPager/PagerContent.swift b/Sources/SwiftUIPager/PagerContent.swift index 275c2ee..82fba12 100644 --- a/Sources/SwiftUIPager/PagerContent.swift +++ b/Sources/SwiftUIPager/PagerContent.swift @@ -92,6 +92,9 @@ extension Pager { /// `true` if `Pager` can be dragged var allowsDragging: Bool = true + /// `true` if `Pager`interacts with the digital crown + var allowsDigitalCrownRotation: Bool = true + /// `true` if pages should have a 3D rotation effect var shouldRotate: Bool = false @@ -245,7 +248,7 @@ extension Pager { #endif #if os(watchOS) - if #available(watchOS 7.0, *) { + if #available(watchOS 7.0, *), allowsDigitalCrownRotation { resultView = resultView .focusable() .digitalCrownRotation(