-
Notifications
You must be signed in to change notification settings - Fork 391
[开发中]Swiper 支持bindtransition 和 bindanimationfinish #2391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiao19880917lu
wants to merge
1
commit into
master
Choose a base branch
from
feat-swiper-eventhandle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,11 @@ type EventDataType = { | |
| // onUpdate时根据上一个判断方向,onFinalize根据transformStart判断 | ||
| transdir: number | ||
| } | ||
| // bindtransition回调的参数 | ||
| type EventTransition = { | ||
| dx: number | ||
| dy: number | ||
| } | ||
|
|
||
| interface SwiperProps { | ||
| children?: ReactNode | ||
|
|
@@ -74,6 +79,8 @@ interface SwiperProps { | |
| 'simultaneous-handlers'?: Array<GestureHandler> | ||
| disableGesture?: boolean | ||
| bindchange?: (event: NativeSyntheticEvent<TouchEvent> | unknown) => void | ||
| bindtransition?: (event: NativeSyntheticEvent<TouchEvent> | unknown) => void | ||
| bindanimationfinish?: (event: NativeSyntheticEvent<TouchEvent> | unknown) => void | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -154,7 +161,9 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| circular = false, | ||
| disableGesture = false, | ||
| current: propCurrent = 0, | ||
| bindchange | ||
| bindchange, | ||
| bindtransition, | ||
| bindanimationfinish | ||
| } = props | ||
|
|
||
| const dotCommonStyle = { | ||
|
|
@@ -403,6 +412,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| }, () => { | ||
| currentIndex.value = nextIndex | ||
| runOnJS(runOnJSCallback)('loop') | ||
| runOnJS(runOnJSCallback)('handleAnimationfinish', nextIndex) | ||
| }) | ||
| } else { | ||
| // 默认向右, 向下 | ||
|
|
@@ -418,6 +428,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| offset.value = initOffset | ||
| currentIndex.value = nextIndex | ||
| runOnJS(runOnJSCallback)('loop') | ||
| runOnJS(runOnJSCallback)('handleAnimationfinish', nextIndex) | ||
| }) | ||
| } else { | ||
| nextIndex = currentIndex.value + 1 | ||
|
|
@@ -429,6 +440,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| }, () => { | ||
| currentIndex.value = nextIndex | ||
| runOnJS(runOnJSCallback)('loop') | ||
| runOnJS(runOnJSCallback)('handleAnimationfinish', nextIndex) | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -462,12 +474,23 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| bindchange && bindchange(eventData) | ||
| } | ||
| } | ||
| function handleTransition (transData: EventTransition) { | ||
| const eventData = getCustomEvent('change', {}, { detail: transData, layoutRef: layoutRef }) | ||
| bindtransition && bindtransition(eventData) | ||
| } | ||
|
|
||
| function handleAnimationfinish (current: number) { | ||
| const eventData = getCustomEvent('change', {}, { detail: { current, source: 'touch' }, layoutRef: layoutRef }) | ||
| bindanimationfinish && bindanimationfinish(eventData) | ||
| } | ||
|
|
||
| const runOnJSCallbackRef = useRef({ | ||
| loop, | ||
| pauseLoop, | ||
| resumeLoop, | ||
| handleSwiperChange | ||
| handleSwiperChange, | ||
| handleTransition, | ||
| handleAnimationfinish | ||
| }) | ||
| const runOnJSCallback = useRunOnJSCallback(runOnJSCallbackRef) | ||
|
|
||
|
|
@@ -493,6 +516,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| easing: easeMap[easeingFunc] | ||
| }, () => { | ||
| currentIndex.value = propCurrent | ||
| bindanimationfinish && runOnJS(runOnJSCallback)('handleAnimationfinish', propCurrent) | ||
| }) | ||
| } else { | ||
| offset.value = targetOffset | ||
|
|
@@ -513,6 +537,28 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| runOnJS(runOnJSCallback)('handleSwiperChange', newIndex, propCurrent) | ||
| } | ||
| }) | ||
| // todo: 如果通过设置current更改,dx和小程序的dx不对等 | ||
| useAnimatedReaction(() => offset.value, (curOffset, preOffset) => { | ||
| const curAbsOffset = Math.abs(curOffset) | ||
| const preAbsOffset = Math.abs(preOffset || 0) | ||
| const computeOffset = step.value * (currentIndex.value + patchElmNumShared.value) | ||
| // 有小数点的情况 | ||
| const isEqual = Math.abs(Math.floor(computeOffset) - Math.floor(Math.abs(offset.value))) <= 2 | ||
| if (curAbsOffset !== preAbsOffset && curAbsOffset && !isEqual) { | ||
| // 移动的距离,手向左滑动正数( curAbsOffset >= preAbsOffset),右右滑动是负数( curAbsOffset < preAbsOffset) | ||
| let trans = 0 | ||
| if (curAbsOffset >= preAbsOffset) { | ||
| trans = curAbsOffset % step.value | ||
| } else { | ||
| trans = -(step.value - curAbsOffset % step.value) | ||
| } | ||
| const transData = { | ||
| dx: dir === 'x' ? trans : 0, | ||
| dy: dir === 'y' ? trans : 0 | ||
| } | ||
| bindtransition && runOnJS(runOnJSCallback)('handleTransition', transData) | ||
| } | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| let patchStep = 0 | ||
|
|
@@ -640,6 +686,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| currentIndex.value = selectedIndex | ||
| offset.value = resetOffset | ||
| runOnJS(runOnJSCallback)('resumeLoop') | ||
| bindanimationfinish && runOnJS(runOnJSCallback)('handleAnimationfinish', selectedIndex) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上,合并为一个runOnJS |
||
| } | ||
| }) | ||
| } else { | ||
|
|
@@ -650,6 +697,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| if (touchfinish.value !== false) { | ||
| currentIndex.value = selectedIndex | ||
| runOnJS(runOnJSCallback)('resumeLoop') | ||
| bindanimationfinish && runOnJS(runOnJSCallback)('handleAnimationfinish', selectedIndex) | ||
| } | ||
| }) | ||
| } | ||
|
|
@@ -672,6 +720,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr | |
| if (touchfinish.value !== false) { | ||
| currentIndex.value = moveToIndex | ||
| runOnJS(runOnJSCallback)('resumeLoop') | ||
| bindanimationfinish && runOnJS(runOnJSCallback)('handleAnimationfinish', moveToIndex) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥这里没有bindtransition的前置判断
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以及建议将这两个runOnJS合为一个,在js函数里再去分发,而不是调用两次runOnJS