Skip to content

Commit

Permalink
Merge pull request #6 from hugehoge/add-doc-comment
Browse files Browse the repository at this point in the history
Added document comment
  • Loading branch information
hugehoge authored Jan 4, 2022
2 parents ff15e7c + 0215858 commit 1c72bae
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Public/ScrollView+SnappableModifier.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import SwiftUI

public extension ScrollView {
/// Make the ScrollView snappable.
/// - Parameters:
/// - alignment: Alignment that is used as a guide for snapping.
/// - mode: The mode by when ScrollView snaps the items.
/// - Returns: A ScrollView, with the snapping behavior set.
func snappable(
_ alignment: SnapAlignment = .center,
mode: SnapMode = .afterScrolling
Expand Down
1 change: 1 addition & 0 deletions Sources/Public/Types/DecelerationRate.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import UIKit

/// A typealias defining the deceleration rate for ScrollView that be applied `snappable` modifier.
public typealias DecelerationRate = UIScrollView.DecelerationRate
10 changes: 10 additions & 0 deletions Sources/Public/Types/SnapAlignment.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/// An alignment definition in snapping.
public enum SnapAlignment {
/// A snap guide marking the top and leading edges of the view.
@available(*, unavailable, message: "Not yet to support in horizontal + vertical movements.")
case topLeading
/// A snap guide marking the top edge of the view.
case top
/// A snap guide marking the top and trailing edges of the view.
@available(*, unavailable, message: "Not yet to support in horizontal + vertical movements.")
case topTrailing
/// A snap guide marking the leading edge of the view.
case leading
/// A snap guide marking the center of the view.
case center
/// A snap guide marking the trailing edge of the view.
case trailing
/// A snap guide marking the bottom and leading edges of the view.
@available(*, unavailable, message: "Not yet to support in horizontal + vertical movements.")
case bottomLeading
/// A snap guide marking the bottom edge of the view.
case bottom
/// A snap guide marking the bottom and trailing edges of the view.
@available(*, unavailable, message: "Not yet to support in horizontal + vertical movements.")
case bottomTrailing
}
15 changes: 15 additions & 0 deletions Sources/Public/Types/SnapMode.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import UIKit

/// The modes that the snapping behavior of ScrollView.
///
/// It has the following two setting values depending on the snap timing.
/// - `.immediately`
/// - `.afterScrolling`
public struct SnapMode {
/// The default setting for snapping after scrolling.
public static let afterScrolling: SnapMode = afterScrolling(decelerationRate: .fast)
/// The default setting for snapping immediately.
public static let immediately: SnapMode = immediately(decelerationRate: .normal, withFlick: true)

internal let decelerationRate: DecelerationRate
internal let snapTiming: SnapTiming

/// An editable setting for snapping after scrolling.
/// - Parameter decelerationRate: The deceleration rate of the ScrollView after dragging end.
/// - Returns: SnapMode, edited as afterScrolling.
public static func afterScrolling(decelerationRate: DecelerationRate) -> SnapMode {
SnapMode(decelerationRate: decelerationRate, snapTiming: .afterScrolling)
}
/// An editable setting for snapping immediately.
/// - Parameters:
/// - decelerationRate: The deceleration rate of the ScrollView after dragging end.
/// - withFlick: The flag whether or not do snapping in consideration of flicking.
/// - Returns: SnapMode, edited as immediately.
public static func immediately(decelerationRate: DecelerationRate, withFlick: Bool = true) -> SnapMode {
SnapMode(decelerationRate: decelerationRate, snapTiming: .immediately(withFlick: withFlick))
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/Public/View+SnappableIDModifier.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import SwiftUI

public extension View {
/// Make the View a snap target.
///
/// `snapID(_:)` applies [id(\_:)](https://developer.apple.com/documentation/swiftui/view/id(_:))
/// modifier internally. Therefore, there is no need to apply `id(_:)` modifier additionally, and if you do, it will happen
/// an unexpected behavior of snapping.
///
/// - Returns: A View, set as a snap target.
func snapID<ID>(_ id: ID) -> some View where ID: Hashable {
self.modifier(SnapIDModifier(id: id))
}
Expand Down

0 comments on commit 1c72bae

Please sign in to comment.