2016-11-30 62 views
0

我想在Xamarin.Forms(版本2.3.3.168)中動畫圖像。動畫正在工作,但不會重複。Xamarin格式動畫不重複

public class WelcomePage : ContentPage 
{ 
    Image img; 

    public WelcomePage() 
    { 
     img = new Image 
     { 
      Source = "img.png", 
      HorizontalOptions = LayoutOptions.Center, 
      VerticalOptions = LayoutOptions.Center, 
     }; 

     Content = new StackLayout 
     { 
      VerticalOptions = LayoutOptions.Center, 
      Children = { 
       img 
      } 
     }; 
    } 

    protected override void OnAppearing() 
    { 
     base.OnAppearing(); 

     var a = new Animation(); 
     a.Add(0, 0.5, new Animation((v) => 
     { 
      img.Scale = v; 
     }, 1.0, 1.2, Easing.CubicInOut,() => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); })); 
     a.Add(0.5, 1, new Animation((v) => 
     { 
      img.Scale = v; 
     }, 1.2, 1.0, Easing.CubicInOut,() => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); })); 
     a.Commit(img, "animation", 16, 2000, Easing.Linear, (d, f) => img.Scale = 1.0,() => 
     { 
      System.Diagnostics.Debug.WriteLine("ANIMATION ALL"); 
      return true; 
     }); 
    } 
} 

運行的應用程序幾秒鐘後,下面的調試輸出打印:

ANIMATION A 
ANIMATION B 
ANIMATION ALL 
ANIMATION ALL 
ANIMATION ALL 

我測試這是一個UWP。

回答

1

根據this thread on the Xamarin forums,其他人似乎也有同樣的問題。這似乎與每個子動畫中設置的私有財產有關。

一種解決方法是每個鏈完成時間來重新創建動畫鏈:

public class WelcomePage : ContentPage 
{ 
    Image img; 

    public WelcomePage() 
    { 
     img = new Image 
     { 
      Source = "circle_plus.png", 
      HorizontalOptions = LayoutOptions.Center, 
      VerticalOptions = LayoutOptions.Center, 
     }; 

     Content = new StackLayout 
     { 
      VerticalOptions = LayoutOptions.Center, 
      Children = { 
       img 
      } 
     }; 
    } 

    protected override void OnAppearing() 
    { 
     base.OnAppearing(); 
     animate(); 
    } 

    void animate() 
    { 
     var a = new Animation(); 
     a.Add(0, 0.5, new Animation((v) => 
     { 
      img.Scale = v; 
     }, 1.0, 1.2, Easing.CubicInOut,() => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); })); 
     a.Add(0.5, 1, new Animation((v) => 
     { 
      img.Scale = v; 
     }, 1.2, 1.0, Easing.CubicInOut,() => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); })); 
     a.Commit(img, "animation", 16, 2000, null, (d, f) => 
     { 
      img.Scale = 1.0; 
      System.Diagnostics.Debug.WriteLine("ANIMATION ALL"); 
      animate(); 
     }); 
    } 
}