Skip to content

Commit 2ca6c17

Browse files
authored
refactor(Overscroll): Refactor overscroll physics (#138)
* Refactors the overscroll effect to use a new damping calculation based on window size, replacing the previous `parabolaScrollEasing` function. * Updates the `scrollEasing` parameter signature to take `distance` and `range` instead of `currentOffset` and `newOffset`. * Adjusts the spring stiffness value for a different feel. * Rewrites the `NestedScrollConnection` implementation to track touch delta and calculate damped offsets, improving the overscroll behavior for scroll and fling gestures.
1 parent 233ade0 commit 2ca6c17

File tree

5 files changed

+137
-125
lines changed

5 files changed

+137
-125
lines changed

docs/guide/utils.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Normally, you don't need to use it actively. See the [ListPopup](../components/l
5353

5454
## Overscroll Effects
5555

56-
Miuix provides easy-to-use overscroll effects modifier for smoother and more natural scrolling experiences.
56+
Miuix provides easy-to-use overscroll effect modifiers for smoother and more natural scrolling experiences.
5757

5858
### Vertical Overscroll
5959

@@ -81,19 +81,19 @@ LazyRow(
8181

8282
### Custom Overscroll Parameters
8383

84-
You can customize the parameters of the overscroll effect to meet specific requirements.
84+
You can customize the overscroll effect parameters as needed:
8585

8686
```kotlin
8787
LazyColumn(
8888
modifier = Modifier.overScrollVertical(
89-
nestedScrollToParent = true, // Whether to dispatch nested scroll events to the parent, default is true
90-
scrollEasing = { currentOffset, newOffset -> // Custom scroll easing function
91-
parabolaScrollEasing(currentOffset, newOffset, p = 25f, density = LocalDensity.current.density)
89+
nestedScrollToParent = true, // Dispatch nested scroll events to parent, default true
90+
scrollEasing = { distance, range -> // Custom easing, default effect is similar to HyperOS feel
91+
// Example: DefaultParabolaScrollEasing(distance, range)
9292
},
93-
springStiff = 200f, // Spring stiffness for the rebound animation, default is 200f
94-
springDamp = 1f, // Spring damping for the rebound animation, default is 1f
95-
isEnabled = { platform() == Platform.Android || platform() == Platform.IOS } // Whether to enable the overscroll effect, enabled by default on Android and iOS
96-
),
93+
springStiff = 280f, // Spring stiffness, default 280f
94+
springDamp = 1f, // Spring damping, default 1f
95+
isEnabled = { platform() == Platform.Android || platform() == Platform.IOS } // Enable only on Android/iOS by default
96+
),
9797
overscrollEffect = null // It is recommended to set this parameter to null to disable the default effect
9898
) {
9999
// List content
@@ -102,11 +102,11 @@ LazyColumn(
102102

103103
**Parameter Explanations:**
104104

105-
* `nestedScrollToParent`: Boolean, controls whether nested scroll events (e.g., from parent scroll containers) are dispatched to the parent. Defaults to `true`.
106-
* `scrollEasing`: A function that defines the easing effect when scrolling beyond the bounds. It takes the current offset (`currentOffset`) and the new offset delta (`newOffset`) as parameters and returns the calculated new offset. By default, it uses `parabolaScrollEasing`, providing an iOS-like damping effect.
107-
* `springStiff`: Float, defines the spring stiffness for the rebound animation. Higher values result in a faster and stiffer rebound. Defaults to `200f`.
108-
* `springDamp`: Float, defines the spring damping for the rebound animation. Higher values result in less oscillation. Defaults to `1f`.
109-
* `isEnabled`: A lambda expression returning a Boolean, used to dynamically control whether the overscroll effect is enabled. By default, it is enabled only on Android and iOS platforms.
105+
* `nestedScrollToParent`: Boolean, whether to dispatch nested scroll events to parent. Default: `true`.
106+
* `scrollEasing`: Function `(distance: Float, range: Int) -> Float`, custom easing, default effect is similar to HyperOS feel.
107+
* `springStiff`: Float, spring stiffness for rebound. Default: `280f`.
108+
* `springDamp`: Float, spring damping for rebound. Default: `1f`.
109+
* `isEnabled`: Lambda, whether to enable overscroll. Default: only Android/iOS.
110110

111111
## Scroll End Haptic Feedback (Modifier.scrollEndHaptic())
112112

docs/zh_CN/guide/utils.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ LazyRow(
8181

8282
### 自定义越界回弹参数
8383

84-
您可以自定义越界回弹效果的参数,以满足特定的需求。
84+
您可以根据需要自定义越界回弹效果的参数:
8585

8686
```kotlin
8787
LazyColumn(
8888
modifier = Modifier.overScrollVertical(
89-
nestedScrollToParent = true, // 是否将嵌套滚动事件分发给父级,默认为 true
90-
scrollEasing = { currentOffset, newOffset -> // 自定义滚动缓动函数
91-
parabolaScrollEasing(currentOffset, newOffset, p = 25f, density = LocalDensity.current.density)
89+
nestedScrollToParent = true, // 是否分发嵌套滚动事件到父级,默认 true
90+
scrollEasing = { distance, range -> // 自定义回弹阻尼,默认效果类似 HyperOS 手感
91+
// 示例:DefaultParabolaScrollEasing(distance, range)
9292
},
93-
springStiff = 200f, // 回弹动画的弹簧刚度,默认为 200f
94-
springDamp = 1f, // 回弹动画的弹簧阻尼,默认为 1f
95-
isEnabled = { platform() == Platform.Android || platform() == Platform.IOS } // 是否启用越界回弹效果,默认在 AndroidiOS 上启用
96-
),
93+
springStiff = 280f, // 回弹弹性系数,默认 280f
94+
springDamp = 1f, // 回弹阻尼系数,默认 1f
95+
isEnabled = { platform() == Platform.Android || platform() == Platform.IOS } // 默认仅在 Android/iOS 启用
96+
),
9797
overscrollEffect = null // 建议将此参数设置为 null,禁用默认效果
9898
) {
9999
// 列表内容
@@ -102,11 +102,11 @@ LazyColumn(
102102

103103
**参数说明:**
104104

105-
* `nestedScrollToParent`: 布尔值,控制是否将嵌套滚动事件(如父级滚动容器)传递给父级。默认为 `true`
106-
* `scrollEasing`: 一个函数,用于定义滚动超出边界时的缓动效果。它接收当前偏移量 (`currentOffset`) 和新的偏移量增量 (`newOffset`) 作为参数,并返回计算后的新偏移量。默认使用 `parabolaScrollEasing`,提供类似 iOS 的阻尼效果
107-
* `springStiff`: 浮点数,定义回弹动画的弹簧刚度。值越高,回弹越快越硬。默认为 `200f`
108-
* `springDamp`: 浮点数,定义回弹动画的弹簧阻尼。值越高,振荡越小。默认为 `1f`
109-
* `isEnabled`: 一个返回布尔值的 Lambda 表达式,用于动态控制是否启用越界回弹效果。默认情况下,仅在 AndroidiOS 平台上启用
105+
* `nestedScrollToParent`布尔值,是否分发嵌套滚动事件到父级。默认:`true`
106+
* `scrollEasing`:函数 `(distance: Float, range: Int) -> Float`,自定义回弹阻尼,默认效果类似 HyperOS 手感
107+
* `springStiff`浮点数,回弹弹性系数。默认:`280f`
108+
* `springDamp`浮点数,回弹阻尼系数。默认:`1f`
109+
* `isEnabled`Lambda,是否启用越界回弹。默认仅 Android/iOS。
110110

111111
## 滚动到边界触觉反馈 (Modifier.scrollEndHaptic())
112112

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/G2RoundedCornerShape.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package top.yukonga.miuix.kmp.utils
55

6-
// Form https://github.com/Kyant0/Capsule
6+
// Based on https://github.com/Kyant0/Capsule
77

88
import androidx.annotation.FloatRange
99
import androidx.annotation.IntRange

0 commit comments

Comments
 (0)