프로그래밍/WPF, Silverlight

[WPF] 3.X 버전에서 동적으로 트랜스폼(Transform) 지정하기

당근천국 2012. 1. 29. 08:30
이전 글에서 4.0이후버전에서 트랜스폼을 지정해 봤는데요 이전버전에서는 트랜스폼그룹(TransformGroup)을 생성하여 사용하면 됩니다.
(참고 : [WPF/Silverlight] 4.0이후 버전에서 동적으로 트랜스폼(Transform) 지정하기)

한땀 한땀 원하는 트랜스폼을 추가해야 하죠.


private void StartAni(Button btnMe)
{
    //스토리보드
    Storyboard story = new Storyboard();

    DoubleAnimation daTemp = null;

    //트랜스폼 추가

    //랜드트랜스폼 생성
    TransformGroup tg = new TransformGroup();
    tg.Children.Add(new TranslateTransform());
    tg.Children.Add(new ScaleTransform());
    tg.Children.Add(new RotateTransform());
    tg.Children.Add(new SkewTransform());
    //랜드트랜스폼 지정
    btnMe.RenderTransform = tg;

    //1.시작 지점
    //X축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 20;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(1.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));
    story.Children.Add(daTemp);
    //Y축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 20;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(1.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));
    story.Children.Add(daTemp);
    //X축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 1;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(1.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"));
    story.Children.Add(daTemp);
    //Y축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 1;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(1.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"));
    story.Children.Add(daTemp);
    //각도 조절
    daTemp = new DoubleAnimation();
    daTemp.To = 0;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(1.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
    story.Children.Add(daTemp);


    //2.목표지점
    //X축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 200;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));
    story.Children.Add(daTemp);
    //Y축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 200;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));
    story.Children.Add(daTemp);
    //X축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 2;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"));
    story.Children.Add(daTemp);
    //Y축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 2;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"));
    story.Children.Add(daTemp);
    //각도 조절
    daTemp = new DoubleAnimation();
    daTemp.To = 90;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
    story.Children.Add(daTemp);

    //3. 원점으로
    //X축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 20;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(3.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));
    story.Children.Add(daTemp);
    //Y축 위치
    daTemp = new DoubleAnimation();
    daTemp.To = 20;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(3.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));
    story.Children.Add(daTemp);
    //X축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 1;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(3.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"));
    story.Children.Add(daTemp);
    //Y축 스케일
    daTemp = new DoubleAnimation();
    daTemp.To = 1;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(3.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"));
    story.Children.Add(daTemp);
    //각도 조절
    daTemp = new DoubleAnimation();
    daTemp.To = 0;
    daTemp.Duration = new Duration(TimeSpan.FromSeconds(3.0));
    Storyboard.SetTarget(daTemp, btnMe);
    Storyboard.SetTargetProperty(daTemp, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"));
    story.Children.Add(daTemp);

    story.Begin();
}


차~암 안쉽조잉?
여전이 블랜드가 없으면 프로퍼티패스(PropertyPath)를 직접 생성하기가 힘드므로 동적으로 생성해도 힘들긴 마찬가지입니다;;
여튼 코드로보자면 프로퍼티패스에 들어가는 배열은 트랜스폼그룹에 추가(add)시킨순서인것을 알수 있습니다.

복잡해보이긴하지만 결국 패턴이 똑같아서 별의미는 없습니다 ㅎㅎㅎ