使用 nativescript vuejs 具有淡入淡出效果的 UIScrollView

UIScrollView with fade effect with nativescript vuejs

我想知道是否可以使用 nativescript 使 UIScrollView 具有淡入淡出效果?

例如:https://medium.com/@luisfmachado/uiscrollview-with-fade-effect-246e332e8b24

我阅读了文档 https://nativescript-vue.org/en/docs/elements/components/scroll-view/,但没有找到此信息。

我想要这个结果,例如:

你有什么想法吗?谢谢

我不知道如何将本机代码放入我的组件中

<template>
    <ScrollView class="scroll" orientation="vertical" row="1" ref="scrollView">
        <StackLayout marginLeft="10" marginRight="10" class="container-verses">
            <StackLayout horizontalAlignment="center">
                <Label textWrap="true" textAlignment="center" text="hello" color="#FFFFFF" fontSize="20"/>
                ...
                <Label textWrap="true" textAlignment="center" text="hello" color="#FFFFFF" fontSize="20"/>
            </StackLayout>
        </StackLayout>
    </ScrollView>
</template>

<script>
    export default {
        name    : 'FadeScrollView',
        computed: {},
        methods : {
            //
        }
    };
</script>

<style lang='scss' scoped>

</style>

这是将 Swift 翻译成 NativeScript

的方法
import { isIOS } from "@nativescript/core/platform";
import { ScrollView } from "@nativescript/core/ui/scroll-view";

let FadeScrollViewImpl;

if (isIOS) {
    FadeScrollViewImpl = UIScrollView.extend({
        fadePercentage: 0.2,
        gradientLayer: CAGradientLayer.new(),
        transparentColor: UIColor.clearColor.CGColor,
        opaqueColor: UIColor.blackColor.CGColor,

        topOpacity: () => {
            const scrollViewHeight = this.frame.size.height;
            const scrollContentSizeHeight = this.contentSize.height;
            const scrollOffset = this.contentOffset.y;
            const alpha = (scrollViewHeight >= scrollContentSizeHeight || scrollOffset <= 0) ? 1 : 0;
            return UIColor.alloc().initWithWhiteAlpha(0, alpha).CGColor;
        },

        bottomOpacity: () => {
            const scrollViewHeight = this.frame.size.height;
            const scrollContentSizeHeight = this.contentSize.height;
            const scrollOffset = this.contentOffset.y;
            const alpha = (scrollViewHeight >= scrollContentSizeHeight || scrollOffset + scrollViewHeight >= scrollContentSizeHeight) ? 1 : 0
            return UIColor.alloc().initWithWhiteAlpha(0, alpha).CGColor;
        },

        layoutSubviews() {
            super.layoutSubviews()

            this.delegate = this;
            const maskLayer = CALayer.new();
            maskLayer.frame = this.bounds;

            this.gradientLayer.frame = CGRectMake(this.bounds.origin.x, 0, this.bounds.size.width, this.bounds.size.height);
            this.gradientLayer.colors = [this.topOpacity, this.opaqueColor, this.opaqueColor, this.bottomOpacity];
            this.gradientLayer.locations = [0, NSNumber.alloc().initWithFloat(this.fadePercentage), NSNumber.alloc().initWithFloat(1 - this.fadePercentage), 1];
            maskLayer.addSublayer(this.gradientLayer);

            this.layer.mask = maskLayer
        },

        scrollViewDidScroll(scrollView) {
            this.gradientLayer.colors = [topOpacity, opaqueColor, opaqueColor, bottomOpacity];
        }
    });
}

export class FadeScrollView extends ScrollView {
    createNativeView() {
        if (isIOS) {
            return FadeScrollViewImpl.new();
        } else {
            return super.createNativeView();
        }
    }

    attachNative() {
        if (!isIOS) {
            super.attachNative();
        }
    }
}

然后您只需注册该元素即可开始在模板中使用它

Vue.registerElement('FadeScrollView', () => require('./fade-scrollView').FadeScrollView)

Playground Sample