Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type EventDataType = {
// onUpdate时根据上一个判断方向,onFinalize根据transformStart判断
transdir: number
}
// bindtransition回调的参数
type EventTransition = {
dx: number
dy: number
}

interface SwiperProps {
children?: ReactNode
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -403,6 +412,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
}, () => {
currentIndex.value = nextIndex
runOnJS(runOnJSCallback)('loop')
runOnJS(runOnJSCallback)('handleAnimationfinish', nextIndex)
})
} else {
// 默认向右, 向下
Expand All @@ -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
Expand All @@ -429,6 +440,7 @@ const SwiperWrapper = forwardRef<HandlerRef<View, SwiperProps>, SwiperProps>((pr
}, () => {
currentIndex.value = nextIndex
runOnJS(runOnJSCallback)('loop')
runOnJS(runOnJSCallback)('handleAnimationfinish', nextIndex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥这里没有bindtransition的前置判断

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以及建议将这两个runOnJS合为一个,在js函数里再去分发,而不是调用两次runOnJS

})
}
}
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,合并为一个runOnJS

}
})
} else {
Expand All @@ -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)
}
})
}
Expand All @@ -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)
}
})
}
Expand Down