Skip to content

Commit

Permalink
Add support for watchOS digitalCrown
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Apr 5, 2022
1 parent c018a9c commit b1eb844
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/SwiftUIPager/Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ extension Page {
/// Will move to the first page
case moveToFirst

/// Will increment or decrement the `index` by the passed argument
case move(increment: Int)

/// Will move to the last page
case moveToLast

Expand Down Expand Up @@ -116,6 +119,8 @@ extension Page {
index = 0
case .moveToLast:
index = totalPages - 1
case .move(let increment):
index += increment
case .new(let newIndex):
index = newIndex
}
Expand Down
32 changes: 32 additions & 0 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ extension Pager {
@GestureState var isGestureFinished = true
#endif

#if os(watchOS)

/// Digital Crown offset
@State var digitalCrownPageOffset: CGFloat = 0

/// Digital Crown offset
@State var lastDigitalCrownPageOffset: CGFloat = 0

#endif

/// Initializes a new `Pager`.
///
/// - Parameter size: Available size
Expand Down Expand Up @@ -235,6 +245,28 @@ extension Pager {
}
#endif

#if os(watchOS)
resultView = resultView
.focusable()
.digitalCrownRotation(
$digitalCrownPageOffset,
from: 0,
through: CGFloat(numberOfPages - 1),
by: 1,
sensitivity: .low
)
.onChange(of: digitalCrownPageOffset) { newValue in
print(newValue)
let increment = min(1, max(-1, Int(newValue - lastDigitalCrownPageOffset)))
guard abs(increment) > 0 else { return }
lastDigitalCrownPageOffset = newValue
withAnimation {
pagerModel.update(.move(increment: increment))
}
}
.eraseToAny()
#endif

return resultView.contentShape(Rectangle())
}
}
Expand Down

0 comments on commit b1eb844

Please sign in to comment.