>  기사  >  백엔드 개발  >  WPF에서 GridLengthAnimation 사용자 정의에 대한 예제 튜토리얼

WPF에서 GridLengthAnimation 사용자 정의에 대한 예제 튜토리얼

零下一度
零下一度원래의
2017-05-24 17:31:501553검색

이 글에서는 주로 WPF에서 GridLengthAnimation을 사용자 정의하는 관련 정보를 자세히 소개합니다. 관심 있는 친구는

요구 사항

목록의 항목을 편집할 때 편집된 내용을 오른쪽 등 앞에 배치하고 싶습니다.

이 요구 사항은 그리드를 두 개의 클라우드로 나누고 두 클라우드의

너비를 동적으로 조정하여 달성할 수 있습니다.

클로문의 너비가 이라는 것을 알고 있지만 기본 애니메이션은 이렇지 않습니다. 이런 1인 애니메이션을 우리가 직접 구현해야 합니다.

디자인
애니메이션 클래스 다이어그램에서 봅니다

요구사항부터 시작합니다

목록의 항목을 편집할 때 편집된 내용을 오른쪽 등 앞에 배치하고 싶습니다.

이 요구 사항은 그리드를 두 개의 클라우드로 나누고 두 클라우드의 너비를 동적으로 조정하여 달성할 수 있습니다.

Clomun의 Width가 GridLength인 것을 알고 있지만 기본 애니메이션은 이와 같지 않습니다. 이런 1인 애니메이션을 우리가 직접 구현해야 합니다.

디자인

애니메이션 클래스 다이어그램에서 AnimationTimeline 상속을 확인하고 해당 GetCurrentValue

public class GridLengthAnimation : AnimationTimeline
  {
    /// <summary>
    /// Returns the type of object to animate
    /// </summary>
    public override Type TargetPropertyType => typeof(GridLength);
 
    /// <summary>
    /// Creates an instance of the animation object
    /// </summary>
    /// <returns>Returns the instance of the GridLengthAnimation</returns>
    protected override System.Windows.Freezable CreateInstanceCore()
    {
      return new GridLengthAnimation();
    }
 
    /// <summary>
    /// Dependency property for the From property
    /// </summary>
    public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength),
      typeof(GridLengthAnimation));
 
    /// <summary>
    /// CLR Wrapper for the From depenendency property
    /// </summary>
    public GridLength From
    {
      get
      {
        return (GridLength)GetValue(GridLengthAnimation.FromProperty);
      }
      set
      {
        SetValue(GridLengthAnimation.FromProperty, value);
      }
    }
 
    /// <summary>
    /// Dependency property for the To property
    /// </summary>
    public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength),
      typeof(GridLengthAnimation));
 
    /// <summary>
    /// CLR Wrapper for the To property
    /// </summary>
    public GridLength To
    {
      get
      {
        return (GridLength)GetValue(GridLengthAnimation.ToProperty);
      }
      set
      {
        SetValue(GridLengthAnimation.ToProperty, value);
      }
    }
 
    /// <summary>
    /// Animates the grid let set
    /// </summary>
    /// <param name="defaultOriginValue">The original value to animate</param>
    /// <param name="defaultDestinationValue">The final value</param>
    /// <param name="animationClock">The animation clock (timer)</param>
    /// <returns>Returns the new grid length to set</returns>
    public override object GetCurrentValue(object defaultOriginValue,
      object defaultDestinationValue, AnimationClock animationClock)
    {
      double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
 
      double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
 
      if (fromVal > toVal)
        return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star);
      else
        return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star);
    }

를 재정의합니다. 위에 표시된 대로 기본 애니메이션을 모방합니다. From 및 To를 구현하고

속성 을 GridLength로 정의합니다. 애니메이션이 실행되면 GetCurrentValue를 다시 작성하여 From/To 속성에 따라 연결합니다.

최적화

위의 코드를 통해 GridLength가 변경될 때 애니메이션을 구현할 수 있습니다. 그러나 시도해 본 후에는 애니메이션이 약간 너무 선형적이라는 것을 발견했습니다. 이때 어떻게 해야 합니까?

은 EasingFunction을 도입하여 구현할 수 있습니다. 우리는 EasingFunction이 실제로 시간 t와 관련된 시간

함수 f(t)라는 것을 알고 있습니다. 시간 함수 처리를 통해 애니메이션 전환을 덜 선형적으로 만듭니다.


 /// <summary>
    /// The <see cref="EasingFunction" /> dependency property&#39;s name.
    /// </summary>
    public const string EasingFunctionPropertyName = "EasingFunction";
 
    /// <summary>
    /// Gets or sets the value of the <see cref="EasingFunction" />
    /// property. This is a dependency property.
    /// </summary>
    public IEasingFunction EasingFunction
    {
      get
      {
        return (IEasingFunction)GetValue(EasingFunctionProperty);
      }
      set
      {
        SetValue(EasingFunctionProperty, value);
      }
    }
 
    /// <summary>
    /// Identifies the <see cref="EasingFunction" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register(
      EasingFunctionPropertyName,
      typeof(IEasingFunction),
      typeof(GridLengthAnimation),
      new UIPropertyMetadata(null));

이에 따라 GetCurrentValue 함수도 다시 작성해야 합니다.


public override object GetCurrentValue(object defaultOriginValue,
      object defaultDestinationValue, AnimationClock animationClock)
    {
      double fromVal = ((GridLength)GetValue(FromProperty)).Value;
 
      double toVal = ((GridLength)GetValue(ToProperty)).Value;
 
      //check that from was set from the caller
      //if (fromVal == 1)
      //  //set the from as the actual value
      //  fromVal = ((GridLength)defaultDestinationValue).Value;
 
      double progress = animationClock.CurrentProgress.Value;
 
      IEasingFunction easingFunction = EasingFunction;
      if (easingFunction != null)
      {
        progress = easingFunction.Ease(progress);
      }
 
 
      if (fromVal > toVal)
        return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star);
 
        return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star);
    }

사용

 <anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>

[관련 추천]

ASP.NET 무료 동영상 튜토리얼

2.

C# WinForm의 단축키 사용자 정의 예, _PHP 튜토리얼

3. WeChat 공개 계정을 공유하여 사용자 정의 메뉴 개발 튜토리얼 예

위 내용은 WPF에서 GridLengthAnimation 사용자 정의에 대한 예제 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.