【问题标题】:How to animate elements inserted and removed in NativeScript's FlexboxLayout?如何为 NativeScript 的 FlexboxLayout 中插入和删除的元素设置动画?
【发布时间】:2017-01-04 03:18:29
【问题描述】:

我试图让一排按钮根据是否添加或删除其他按钮来增长和缩小。 FlexboxLayout 似乎最适合这项工作,但我似乎不知道如何解决这个问题,除了手动操作一段时间内的宽度百分比。

我想我可以在它们上设置flexGrow,然后使用带有visibility: collapse 的类来从根本上删除按钮。 (然后在恢复正常时反转。)这有效,但变化是瞬间的。我想要某种挤压/拉伸动画。

我尝试使用动画,将比例缩小到 0,如下所示。虽然按钮看起来缩小了,但它只是向自身中心收缩,并且仍然保持它所占据的空间(留出一个空隙)。

我在玩这样一个简单的例子:

<FlexboxLayout flexDirection="row">
  <Button text="1" width="25%" flexGrow="1"></Button>
  <Button text="2" width="25%" id="test-button"></Button>
  <Button text="3" width="25%" flexGrow="1"></Button>
  <Button text="4" width="25%" flexGrow="1"></Button>
</FlexboxLayout>

我尝试做这样的事情,我想缩小按钮 #2 消失:

let testButton = this.page.getViewById("test-button");
testButton.animate({
  scale: { x: 0, y: 1},
  duration: 500
});

我也尝试使用关键帧来做到这一点。这似乎没有任何作用。

#test-button {
  animation-name: shrink;
  animation-duration: 2s;
  animation-delay: 1s;
}


@keyframes shrink {
    from { width: 25%; }
    to { width: 0; }
}

我尝试做类似in this answer here for web pages 提到的事情,但这似乎也没有任何作用。

我设法通过使用setTimeout 手动调整宽度的数据绑定使其工作。但我想知道是否可能有一条不同的路线可能更容易管理?不禁想知道我是否在其他尝试中搞砸了。

【问题讨论】:

    标签: nativescript angular2-nativescript


    【解决方案1】:

    您可以在使用按钮visibility 属性时隐藏按钮,并在动画完成时隐藏组件。为了您的方便,我附上了示例代码

    app.component.html

    <FlexboxLayout flexDirection="row">
      <Button backgroundColor="green" text="1" width="25%" flexGrow="1"></Button>
      <Button backgroundColor="blue" (loaded)="buttonLoaded()" text="2" width="25%" id="test-button"></Button>
      <Button backgroundColor="green" text="3" width="25%" flexGrow="1"></Button>
      <Button backgroundColor="blue" text="4" width="25%" flexGrow="1"></Button>
    </FlexboxLayout>
    

    app.component.ts

    import { Component } from "@angular/core";
    import { Page } from "ui/page"
    
    @Component({
        selector: "my-app",
        templateUrl: "app.component.html",
    })
    export class AppComponent {
    
        constructor(private page:Page){}
    
        public buttonLoaded(){
            let testButton = this.page.getViewById("test-button");
                testButton.animate({
                scale: { x: 0, y: 1},
                duration: 500
            })
            .then(()=>{
                let testButton = this.page.getViewById("test-button");
                testButton.visibility='collapse'
            });
        }
    }
    

    【讨论】:

    • 哦,嘿,这是一种方法!我也很欣赏方便的代码! :D 谢谢。唯一的事情是,它更像是一个即时捕捉,将可见性设置为折叠。我不确定我是否也喜欢这种方式,因为它仍然很酷。不过,为了最初的想法,其他元素会以动画方式生长到开放空间中,你认为这是只有数据绑定才能做到的事情吗?我使用数据绑定实现它的方法是缩小一个按钮,然后其他按钮会随着缩小而增长。
    • 此方法也不会以动画或线性方式删除项目,而是隐藏元素,然后突然将所有内容恢复,让所有内容看起来都非常丑陋。
    • 你解释得很好很简单,谢谢伙计!
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多