Skip to content

Commit

Permalink
Added blur
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshwc committed Sep 17, 2024
1 parent 232675d commit 04c90ef
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Sources/SwiftUIPager/Pager+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ extension Pager: Buildable {
mutating(keyPath: \.shouldRotate, value: shouldRotate)
}

/// Call this method to add a blur effect to the pages.
/// - Parameter radius: blur radius
/// - Note: `radius` must be greater than 0
public func interactive(blur radius: CGFloat) -> Self {
mutating(keyPath: \.blurRadius, value: radius)
}

/// Call this method to add a 3D rotation effect.
///
/// - Parameter value: `true` if the pages should have a 3D rotation effect
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
/// Opacity increment applied to unfocused pages
var opacityIncrement: Double?

/// blur radius applied to unfocused pages
var blurRadius: CGFloat?

/// `true` if `Pager` can be dragged
var allowsDragging: Bool = true

Expand Down Expand Up @@ -155,7 +158,7 @@ public struct Pager<Element, ID, PageView>: 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)?

Expand Down Expand Up @@ -206,6 +209,7 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
.interactive(scale: interactiveScale)
.interactive(opacity: opacityIncrement)
.interactive(rotation: shouldRotate)
.interactive(blurRadius: blurRadius)
.pageOffset(pageOffset)
.itemSpacing(itemSpacing)
.itemAspectRatio(itemAspectRatio, alignment: itemAlignment)
Expand Down
10 changes: 9 additions & 1 deletion Sources/SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ extension Pager.PagerContent: Buildable {
mutating(keyPath: \.shouldRotate, value: shouldRotate)
}

/// Call this method to add blur effect to the pages
/// - Parameter radius: radius of the blur effect
/// - Note: This modifier is incompatible with `multiplePagination`
/// - Note: This modifier is incompatible with `partialPagination`
func interactive(blurRadius radius: CGFloat?) -> Self {
mutating(keyPath: \.blurRadius, value: radius)
}

/// Provides an increment to the page index offset. Use this to modify the scroll offset
///
/// - Parameter value: manual offset applied to `Pager`
Expand Down Expand Up @@ -313,7 +321,7 @@ extension Pager.PagerContent: Buildable {
func onPageChanged(_ callback: ((Int) -> Void)?) -> Self {
mutating(keyPath: \.onPageChanged, value: callback)
}

/// Sets some padding on the non-scroll axis
///
/// - Parameter length: padding
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftUIPager/PagerContent+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ extension Pager.PagerContent {
let distance = abs(distance(to: item))
return Double(max(interactiveScale, min(1, 1 - distance * scaleIncrement)))
}

/// Blur radius that applies to a particular item
func blur(for item: PageWrapper<Element, ID>) -> CGFloat {
guard let blurRadius = blurRadius else { return 0 }
let distance = abs(distance(to: item))
return distance == 0 ? 0 : CGFloat(blurRadius)
}

private func distance(to item: PageWrapper<Element, ID>) -> CGFloat {
guard let index: Int = dataDisplayed.firstIndex(of: item) else { return 0 }
Expand Down
10 changes: 8 additions & 2 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ extension Pager {
var interactiveScale: CGFloat = 1

/// Opacity increment applied to unfocused pages
var opacityIncrement: Double?
var opacityIncrement: Double?

/// Blur radius applied to unfocused pages
/// - Note: This is only available on iOS 15 and above
/// - SeeAlso: `interactive(blur:)`
var blurRadius: CGFloat? = 0

/// `true` if `Pager` can be dragged
var allowsDragging: Bool = true
Expand Down Expand Up @@ -133,7 +138,7 @@ extension Pager {

/// Callback invoked when a new page is set
var onPageChanged: ((Int) -> Void)?

/// Callback for a dragging began event
var onDraggingBegan: (() -> Void)?

Expand Down Expand Up @@ -193,6 +198,7 @@ extension Pager {
.rotation3DEffect(self.angle(for: item),
axis: self.axis)
.opacity(opacity(for: item))
.blur(radius: blur(for: item))
}
}
}
Expand Down

0 comments on commit 04c90ef

Please sign in to comment.