【问题标题】:Cannot cancel animation in Xamarin Forms无法取消 Xamarin 表单中的动画
【发布时间】:2020-05-18 14:52:03
【问题描述】:

我知道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?

【问题讨论】:

  • 尝试在主线程Device.BeginInvokeonMainThread(()=&gt; ViewExtensions.CancelAnimation(label)调用这个
  • 那根本没有区别 :-(
  • 可以吗?
  • 抱歉没有早点回来。你的回答@LeoZhu-MSFT 奏效了。

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

当您通过调用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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多