Skip to content

Commit

Permalink
Rename animations to interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Mar 5, 2021
1 parent fba809b commit 91563d2
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 64 deletions.
15 changes: 8 additions & 7 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,22 @@ func pageView(_ page: Int) -> some View {

### Scale

Use `interactive` to add a scale animation effect to those pages that are unfocused, that is, those elements whose index is different from `pageIndex`:
Use `interactive(scale:)` to add a scale animation effect to those pages that are unfocused, that is, those elements whose index is different from `pageIndex`:

```swift
Pager(...)
.interactive(0.8)
.interactive(scale: 0.8)
```

<img src="/resources/usage/interactive-pagers.gif" alt="Interactive pager" height="640"/>

### Faded
### Opacity

Get a interactive fading effect on your items by using `faded`:
Get a interactive fading effect on your items by using `interactive(opacity:)`:

```swift
Pager(...)
.faded(0.4)
.interacive(opacity: 0.4)
.preferredItemSize(CGSize(width: 150, height: 150))
.itemSpacing(10)
```
Expand All @@ -206,12 +206,13 @@ Pager(...)

### Rotation

You can also use `rotation3D` to add a rotation effect to your pages:
You can also use `interactive(rotation:)` to add a rotation effect to your pages:

```swift
Pager(...)
.itemSpacing(10)
.rotation3D()
.interactive(rotation: true)
.interactive(scale: 0.7)
```

<img src="/resources/usage/pager-rotation3D.gif" alt="Rotation 3D" height="640"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct BizarreExampleView: View {
}
.itemSpacing(10)
.horizontal(.rightToLeft)
.interactive(0.8)
.interactive(scale: 0.8)
.itemAspectRatio(0.7)
.background(Color.gray.opacity(0.5))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ struct EmbeddedExampleView: View {
id: \.self) { page in
self.pageView(page)
}
.rotation3D()
.interactive(rotation: true)
.interactive(scale: 0.7)
.interactive(opacity: 0.5)
.itemSpacing(10)
.itemAspectRatio(0.8, alignment: .end)
.padding(8)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Create vertical or horizontal pagers, align the cards, change the direction of t
- [Animations](Documentation/Usage.md#animations)
- [Scale](Documentation/Usage.md#scale)
- [Rotation](Documentation/Usage.md#rotation)
- [Opacity](Documentation/Usage.md#opacity)
- [Loop](Documentation/Usage.md#loop)
- [Page Transitions](Documentation/Usage.md#page-transitions)
- [Add pages on demand](Documentation/Usage.md#add-pages-on-demand)
Expand Down
30 changes: 23 additions & 7 deletions Sources/SwiftUIPager/Pager+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,49 @@ extension Pager: Buildable {
///
/// - Parameter scale: shrink ratio
/// - Note: `scale` must be lower than _1_ and greather than _0_, otherwise it defaults to the previous value
@available(*, deprecated, renamed: "interactive(scale:)")
public func interactive(_ scale: CGFloat) -> Self {
guard !shouldRotate else { return self }
guard scale > 0, scale < 1 else { return self }
return mutating(keyPath: \.interactiveScale, value: scale)
return interactive(scale: scale)
}

/// Call this method to provide a shrink ratio that will apply to the items that are not focused.
///
/// - Parameter ratio: shrink ratio applied to unfocsed items
/// - Note: `ratio` must be lower than _1_ and greather than _0_, otherwise it defaults to the previous value
public func interactive(scale ratio: CGFloat) -> Self {
return mutating(keyPath: \.interactiveScale, value: max(0, min(1, ratio)))
}

/// Call this method to provide an interactive opacity effect to neighboring pages. The further they are
/// from the focused page, the more opacity will be applied
///
/// - Parameter stepPercentage: opacity step increment between each index
/// - Parameter decrement: opacity step increment between each index
///
/// For instance, if the focused index is _3_ and `stepPercentage` is `0.4`,
/// then page _2_ and _4_ will have an opacity of `0.8`, pages _1_ and _5_ will have
/// an opacity of `0.4` and so on.
///
/// - Note: `increment` must be lower than _1_ and greather than _0_
public func faded(_ stepPercentage: Double = 0.4) -> Self {
mutating(keyPath: \.opacityIncrement, value: stepPercentage)
public func interactive(opacity decrement: Double) -> Self {
mutating(keyPath: \.opacityIncrement, value: decrement)
}

/// Call this method to add a 3D rotation effect.
///
/// - Parameter value: `true` if the pages should have a 3D rotation effect
public func interactive(rotation shouldRotate: Bool) -> Self {
mutating(keyPath: \.shouldRotate, value: shouldRotate)
}

/// Call this method to add a 3D rotation effect.
///
/// - Parameter value: `true` if the pages should have a 3D rotation effect
/// - Note: If you call this method, any previous or later call to `interactive` will have no effect.
@available(*, deprecated, renamed: "interactive(rotation:)")
public func rotation3D(_ value: Bool = true) -> Self {
mutating(keyPath: \.interactiveScale, value: value ? rotationInteractiveScale : 1)
.mutating(keyPath: \.shouldRotate, value: value)
interactive(scale: value ? 0.7 : 1)
.interactive(rotation: value)
}

/// Provides an increment to the page index offset. Use this to modify the scroll offset
Expand Down
21 changes: 10 additions & 11 deletions Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import SwiftUI
///
/// # Example #
///
/// Pager(page: $page
/// data: data,
/// content: { index in
/// self.pageView(index)
/// }).interactive(0.8)
/// Pager(
/// page: $page
/// data: data,
/// content: { index in
/// self.pageView(index)
/// })
/// .interactive(scale: 0.8)
/// .itemSpacing(10)
/// .padding(30)
/// .itemAspectRatio(0.6)
Expand All @@ -45,9 +47,6 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
/// Angle of rotation when should rotate
let rotationDegrees: Double = 20

/// Angle of rotation when should rotate
let rotationInteractiveScale: CGFloat = 0.7

/// Axis of rotation when should rotate
let rotationAxis: (x: CGFloat, y: CGFloat, z: CGFloat) = (0, 1, 0)

Expand Down Expand Up @@ -189,8 +188,9 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
.contentLoadingPolicy(contentLoadingPolicy)
.loopPages(isInifinitePager, repeating: loopingCount)
.alignment(alignment)
.interactive(interactiveScale)
.faded(opacityIncrement)
.interactive(scale: interactiveScale)
.interactive(opacity: opacityIncrement)
.interactive(rotation: shouldRotate)
.pageOffset(pageOffset)
.itemSpacing(itemSpacing)
.itemAspectRatio(itemAspectRatio, alignment: itemAlignment)
Expand All @@ -215,7 +215,6 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:

pagerContent = allowsMultiplePagination ? pagerContent.multiplePagination() : pagerContent
pagerContent = isHorizontal ? pagerContent.horizontal(horizontalSwipeDirection) : pagerContent.vertical(verticalSwipeDirection)
pagerContent = shouldRotate ? pagerContent.rotation3D() : pagerContent

if let preferredItemSize = preferredItemSize {
pagerContent = pagerContent.preferredItemSize(preferredItemSize)
Expand Down
22 changes: 11 additions & 11 deletions Sources/SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,32 @@ extension Pager.PagerContent: Buildable {

/// Call this method to provide a shrink ratio that will apply to the items that are not focused.
///
/// - Parameter scale: shrink ratio
/// - Note: `scale` must be lower than _1_ and greather than _0_, otherwise it defaults to the previous value
func interactive(_ scale: CGFloat) -> Self {
mutating(keyPath: \.interactiveScale, value: scale)
/// - Parameter ratio: shrink ratio applied to unfocsed items
/// - Note: `ratio` must be lower than _1_ and greather than _0_, otherwise it defaults to the previous value
func interactive(scale ratio: CGFloat) -> Self {
guard ratio > 0, ratio < 1 else { return self }
return mutating(keyPath: \.interactiveScale, value: ratio)
}

/// Call this method to provide an interactive opacity effect to neighboring pages. The further they are
/// from the focused page, the more opacity will be applied
///
/// - Parameter stepPercentage: opacity step increment between each index
/// - Parameter decrement: opacity step increment between each index
///
/// For instance, if the focused index is _3_ and `stepPercentage` is `0.4`,
/// then page _2_ and _4_ will have an opacity of `0.8`, pages _1_ and _5_ will have
/// an opacity of `0.4` and so on.
///
/// - Note: `increment` must be lower than _1_ and greather than _0_
func faded(_ stepPercentage: Double?) -> Self {
mutating(keyPath: \.opacityIncrement, value: stepPercentage)
func interactive(opacity decrement: Double?) -> Self {
mutating(keyPath: \.opacityIncrement, value: decrement)
}

/// Call this method to add a 3D rotation effect.
///
/// - Parameter value: `true` if the pages should have a 3D rotation effect
/// - Note: If you call this method, any previous or later call to `interactive` will have no effect.
func rotation3D(_ value: Bool = true) -> Self {
mutating(keyPath: \.shouldRotate, value: value)
func interactive(rotation shouldRotate: Bool) -> Self {
mutating(keyPath: \.shouldRotate, value: shouldRotate)
}

/// Provides an increment to the page index offset. Use this to modify the scroll offset
Expand Down
3 changes: 0 additions & 3 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ extension Pager {
/// Angle of rotation when should rotate
let rotationDegrees: Double = 20

/// Angle of rotation when should rotate
let rotationInteractiveScale: CGFloat = 0.7

/// Axis of rotation when should rotate
let rotationAxis: (x: CGFloat, y: CGFloat, z: CGFloat) = (0, 1, 0)

Expand Down
19 changes: 16 additions & 3 deletions Tests/SwiftUIPagerTests/Pager+Buildable_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class Pager_Buildable_Tests: XCTestCase {

func test_GivenPager_WhenFaded_ThenOpacityIncrementChanges() {
var pager = givenPager
pager = pager.faded(0.2)
pager = pager.interactive(opacity: 0.2)

let pagerContent = pager.content(for: CGSize(width: 100, height: 100))
XCTAssertEqual(pagerContent.opacityIncrement, 0.2)
Expand Down Expand Up @@ -264,14 +264,25 @@ final class Pager_Buildable_Tests: XCTestCase {
XCTAssertEqual(pagerContentInteractive.interactiveScale, pagerContentWithRotation.interactiveScale)
XCTAssertEqual(pagerContentInteractive.shouldRotate, pagerContentWithRotation.shouldRotate)
}

func test_GivenPager_WhenCombineInteractiveModifier_ThenNoExclusive() {
let interactivePager = givenPager
.interactive(scale: 0.4)
.interactive(opacity: 0.3)
.interactive(rotation: true)
let pagerContent = interactivePager.content(for: CGSize(width: 100, height: 100))
XCTAssertEqual(pagerContent.opacityIncrement, 0.3)
XCTAssertEqual(pagerContent.interactiveScale, 0.4)
XCTAssertTrue(pagerContent.shouldRotate)
}

func test_GivenPager_When3DRotation_ThenShouldRotate() {
var pager = givenPager
pager = pager.rotation3D()
let pagerContent = pager.content(for: CGSize(width: 100, height: 100))

XCTAssertTrue(pagerContent.shouldRotate)
XCTAssertEqual(pagerContent.interactiveScale, pagerContent.rotationInteractiveScale)
XCTAssertEqual(pagerContent.interactiveScale, 0.7)
}

func test_GivenPagerWith3DRotation_When3DRotationFalse_ThenShouldRotateFalse() {
Expand Down Expand Up @@ -643,7 +654,9 @@ final class Pager_Buildable_Tests: XCTestCase {
("test_GivenPager_WhenOnDraggingChanged_ThenCallback", test_GivenPager_WhenOnDraggingChanged_ThenCallback),
("test_GivenPager_WhenOnDraggingEnded_ThenCallback", test_GivenPager_WhenOnDraggingEnded_ThenCallback),
("test_GivenPager_WhenOnPageChanged_ThenCallbackNotNil", test_GivenPager_WhenOnPageChanged_ThenCallbackNotNil),
("test_GivenPager_WhenFaded_ThenOpacityIncrementChanges", test_GivenPager_WhenFaded_ThenOpacityIncrementChanges)
("test_GivenPager_WhenFaded_ThenOpacityIncrementChanges", test_GivenPager_WhenFaded_ThenOpacityIncrementChanges),
("test_GivenPager_WhenCombineInteractiveModifier_ThenNoExclusive", test_GivenPager_WhenCombineInteractiveModifier_ThenNoExclusive),
("test_GivenPager_WhenCombineInteractiveModifier_ThenNoExclusive", test_GivenPager_WhenCombineInteractiveModifier_ThenNoExclusive)
]
}

Expand Down
Loading

0 comments on commit 91563d2

Please sign in to comment.