Nativescript Gesture- 旋转奇怪的行为

Nativescript Gesture- Rotation strange behavior

我正在使用 Nativescript Vue 并尝试通过 Nativescript-Gesture Rotation 旋转 TextView 标签中的文本。文本旋转,但不平滑,它从一个方向跳到另一个方向。这就是我的问题

Why this happens? What is the reason that NS-Gesture Rotation acts so strange? And how to make it work?

我将 post 我的示例在这里和 NS Playground 中。

<template>
    <Page class="page">
        <ActionBar title="Home" class="action-bar" />
            <StackLayout class="home-panel">
                <TextView @rotation="onRotation" id="liveLabel" textWrap="true"
                    :rotate="textRotation" editable="false">
                    <FormattedString id="formString">
                        <Span id="spanText" text="Test text" fontSize="20"
                            color="red" />
                    </FormattedString>
                </TextView>
            </StackLayout>
    </Page>
</template>

<script>
    export default {
        data() {
            return {
                textRotation: 0
            };
        },
        methods: {
            onRotation(args) {
                console.log(
                    "Rotate angle: " + args.rotation + " state: " + args.state
                );
                this.textRotation = Math.floor(args.rotation);
            }
        }
    };
</script>

实际上,您所看到的完全是预料之中的,并且您正在实现它。

假设您正在计算一个对象的位置并同时移动它,因此 TextView 上的旋转事件会根据您的手指移动一次给出正确的位置,然后根据您的新旋转值给出另一个位置应用于 TextView。

为了解决这个问题,您应该有 2 个对象副本(此处为 TextView)。一个用来听手指运动,另一个用来制作动画,类似这样。

<template>
    <Page class="page">
        <ActionBar title="Home" class="action-bar" />
        <StackLayout class="home-panel">
            <GridLayout>
                <TextView ref="animatedLbl" textWrap="true" :rotate="textRotation"
                    editable="false" visibility="hidden" verticalAlignment="top">
                    <FormattedString>
                        <Span text="Test text" fontSize="20" color="red" />
                    </FormattedString>
                </TextView>
                <TextView ref="hostLbl" @rotation="onRotation" textWrap="true"
                    editable="false" verticalAlignment="top">
                    <FormattedString>
                        <Span text="Test text" fontSize="20" color="red" />
                    </FormattedString>
                </TextView>
            </GridLayout>
        </StackLayout>
    </Page>
</template>

<script>
    import * as GestureModule from "tns-core-modules/ui/gestures";
    export default {
        data() {
            return {
                textRotation: 0
            };
        },
        methods: {
            onRotation(args) {
                if (args.state === GestureModule.GestureStateTypes.began) {
                    this.$refs.hostLbl.nativeView.visibility = "hidden";
                    this.$refs.animatedLbl.nativeView.visibility = "visible";
                }
                this.textRotation = Math.floor(args.rotation);
                if (
                    args.state === GestureModule.GestureStateTypes.cancelled ||
                    args.state === GestureModule.GestureStateTypes.ended
                ) {
                    this.$refs.hostLbl.nativeView.rotate = this.textRotation;
                    this.$refs.hostLbl.nativeView.visibility = "visible";
                    this.$refs.animatedLbl.nativeView.visibility = "hidden";
                }
            }
        }
    };
</script>

Playground Sample