无法取消Xamarin形式的动画

问题描述 投票:0回答:1

我知道ViewExtensions.CancelAnimations(view);。我已经试过了没用

我已经在Xamarin Forms Github repo上提出了这个问题,同时我希望有人可以提供一种解决方法。

我正在尝试在由ViewModel属性启动和停止的Label上创建比例动画。我已经创建了两个简单的TriggerAction<VisualElement>来开始和停止

public class StartAnimationAction : TriggerAction<VisualElement>
{
    public Animation Animation { set; get; }

    protected override void Invoke(VisualElement view)
    {
        this.Animation.Commit(view, "ScaleIt", length: 30000, easing: Easing.Linear,
                finished: (v, c) => view.Scale = 1, repeat: () => false);
    }
}

public class StopAnimationAction : TriggerAction<VisualElement>
{
    protected override void Invoke(VisualElement view)
    {
        ViewExtensions.CancelAnimations(view);
    }
}

[StartAction上的动画属性是通过View设置的

var throbAnimation = new Animation(v => this.MyLabel.Scale = v, 1, 2);

[ViewModel通过DataTrigger完成

<Label x:Name="MyLabel" Text="Hello World">
    <Label.Triggers>
        <DataTrigger TargetType="Label" Binding="{Binding IsSyncing}" Value="True">
            <DataTrigger.EnterActions>
                <settings:StartAnimationAction x:Name="StartAnimation"/>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <settings:StopAnimationAction/>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Label.Triggers>
</Label>

ViewExtensions.CancelAnimations不会停止动画,并且继续调用回调和重复回调。

还有其他取消动画的方法,或者我是否错误地使用CancelAnimations?

xamarin xamarin.forms xamarin.ios
1个回答
0
投票

当您通过调用Commit方法获得动画时,可以通过调用AbortAnimation扩展方法来取消动画。

更改

public class StopAnimationAction : TriggerAction<VisualElement>
{
  protected override void Invoke(VisualElement view)
  {
    ViewExtensions.CancelAnimations(view);
  }
}

public class StopAnimationAction : TriggerAction<VisualElement>
{
    protected override void Invoke(VisualElement view)
    {
      view.AbortAnimation("ScaleIt");//the name you commit in startAnimationAciton
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.