|
| 1 | +import mpx, { getMixin } from '@mpxjs/core' |
| 2 | + |
| 3 | +let mixin = {} as Parameters<typeof getMixin>[0] |
| 4 | +// eslint-disable-next-line |
| 5 | +// @ts-ignore |
| 6 | +if (__mpx_mode__ === 'ios' || __mpx_mode__ === 'android') { |
| 7 | + type ANIMATION_PRESET = Record< |
| 8 | + string, |
| 9 | + ( |
| 10 | + animationOptions: WechatMiniprogram.AnimationOption, |
| 11 | + ) => void |
| 12 | + > |
| 13 | + mixin = { |
| 14 | + data: { |
| 15 | + ANIMATION_PRESET: {} as ANIMATION_PRESET, |
| 16 | + switchAnimationData: {} as WechatMiniprogram.AnimationExportResult, |
| 17 | + switchHandleAnimationData: {} as WechatMiniprogram.AnimationExportResult |
| 18 | + }, |
| 19 | + lifetimes: { |
| 20 | + created() { |
| 21 | + this.ANIMATION_PRESET = { |
| 22 | + 'cube-switch-BGC': (animationOptions) => { |
| 23 | + const animation = this.maskAnimation || (this.maskAnimation = mpx.createAnimation({ ...animationOptions })) |
| 24 | + if (this.isOn) { |
| 25 | + const onBGC = this.switchOnGBC || '#FF6435' |
| 26 | + animation.backgroundColor(onBGC).step() |
| 27 | + } else { |
| 28 | + const defaultBGC = this.switchDefaultGBC || '#EAEAEA' |
| 29 | + animation.backgroundColor(defaultBGC).step() |
| 30 | + } |
| 31 | + this.switchAnimationData = animation.export() |
| 32 | + }, |
| 33 | + 'cube-switch-left': (animationOptions) => { |
| 34 | + const animation = this.maskAnimation || (this.maskAnimation = mpx.createAnimation({ ...animationOptions })) |
| 35 | + if (this.isOn) { |
| 36 | + animation.left(18).step() |
| 37 | + } else { |
| 38 | + animation.left(2).step() |
| 39 | + } |
| 40 | + this.switchHandleAnimationData = animation.export() |
| 41 | + } |
| 42 | + } |
| 43 | + this.$watch( |
| 44 | + 'isOn', |
| 45 | + (n, o) => { |
| 46 | + if (!!n === !!o) return |
| 47 | + this.rnAnimation({ |
| 48 | + duration: 300, |
| 49 | + timingFunction: 'ease' |
| 50 | + }) |
| 51 | + }, |
| 52 | + { immediate: true } |
| 53 | + ) |
| 54 | + } |
| 55 | + }, |
| 56 | + methods: { |
| 57 | + async rnAnimation( |
| 58 | + animationOptions: WechatMiniprogram.StepOption = {} |
| 59 | + ) { |
| 60 | + this.ANIMATION_PRESET['cube-switch-BGC'](animationOptions) |
| 61 | + this.ANIMATION_PRESET['cube-switch-left'](animationOptions) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +export default getMixin(mixin) |
0 commit comments