UIScrollView с эффектом затухания с nativescript vuejs - PullRequest
3 голосов
/ 14 апреля 2020

Я хотел бы знать, возможно ли иметь UIScrollView с эффектом затухания с помощью nativescript, пожалуйста?

Например: https://medium.com/@luisfmachado / uiscrollview-with-fade-effect-246e332e8b24

Я прочитал документацию https://nativescript-vue.org/en/docs/elements/components/scroll-view/, но я не нашел эту информацию.

Я хотел бы получить такой результат, например:

enter image description here

У вас есть идея, пожалуйста? Спасибо

Понятия не имею, как я могу добавить собственный код в свой компонент

<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>

1 Ответ

6 голосов
/ 26 апреля 2020

Вот как вы переводите 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)

Образец игровой площадки

...