2012년 7월 22일 일요일

[WPF] 에니메이션 코드로 구동시키기(Storyboard)

ㆍ언어대상 : WPF   ㆍ실행대상 : WPF   ㆍ작성자 : 홍석인  

※ 본 예제는 버튼에 투명도와 동시에 높이가 증가하는 에니메이션 증가한 소스입니다.

private Storyboard myStoryboard; // 전역변수로 선언한다.

    private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            StackPanel myPanel = new StackPanel();
            myPanel.Margin = new Thickness(10);

            // 사각형을 생성합니다.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Margin = new Thickness(0);
            myRectangle.Name = "myRectangle";
            myRectangle.Width = 100;
            myRectangle.Height = 100;
            myRectangle.Fill = Brushes.Red;
            this.RegisterName(myRectangle.Name, myRectangle);

            // 버튼을 생성합니다. 하지만 임의 편집기로 버튼을 생성한 경우에는 this.RegisterName(btn.Name, btn); 레지스터에 등록만해줍니다.
            Button btn = new Button();
            btn.Name = "ButtonTest";
            btn.Margin = new Thickness(0, 0, 0, 0);
            btn.Width = 50;
            btn.Height = 50;
            btn.Background = Brushes.Black;
           
            this.RegisterName(btn.Name, btn); // 레지스터에 등록 실행 할 객체의 이름을 알기 위하여

            #region 다양한 에니메이션 정의
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 1.0;  // 시작
            myDoubleAnimation.To = 0.0;    // 끝
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(2)); // 실행할 에니메이션 시간
            myDoubleAnimation.AutoReverse = true; // 반복여부
            myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever; // 반복횟수

            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
            myDoubleAnimation1.To = 0;
            myDoubleAnimation1.Duration = new Duration(TimeSpan.FromSeconds(2));
            myDoubleAnimation1.AutoReverse = true;
            myDoubleAnimation1.RepeatBehavior = RepeatBehavior.Forever;
            #endregion

            myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation); // 스토리보드에 등록
            myStoryboard.Children.Add(myDoubleAnimation1);

            Storyboard.SetTargetName(myDoubleAnimation, btn.Name); // 실행할 에니메이션의 객체 설정
            Storyboard.SetTargetName(myDoubleAnimation1, btn.Name);

            // 에니메이션을 적용 할 프로퍼트 설정
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Button.OpacityProperty));
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(Button.HeightProperty));

            myPanel.Children.Add(myRectangle);
            myPanel.Children.Add(btn);

            myStoryboard.Begin(this); // 스토리보드를 실행 시킵니다.
        }

// Transform에 관련된 에니메이션

 str.Children.Clear();

                TranslateTransform trans = new TranslateTransform(0, YPostion);
                canvas1.RenderTransform = trans;
  
                DoubleAnimation X_move = new DoubleAnimation(0, this.ActualWidth + canvas1.Width, TimeSpan.FromSeconds(Time));
                DoubleAnimation Y_move = new DoubleAnimation(YPostion, TimeSpan.FromSeconds(Time));
 
                X_move.RepeatBehavior = Y_move.RepeatBehavior = RepeatBehavior.Forever;

                str.Children.Add(X_move);
                str.Children.Add(Y_move);

                Storyboard.SetTargetProperty(X_move, new PropertyPath("(0).(1)", new DependencyProperty[] { UIElement.RenderTransformProperty, TranslateTransform.XProperty }));
                Storyboard.SetTargetProperty(Y_move, new PropertyPath("(0).(1)", new DependencyProperty[] { UIElement.RenderTransformProperty, TranslateTransform.YProperty }));
                Storyboard.SetTarget(str, canvas1);

                str.Begin();


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


using System.Windows.Media.Animation;

namespace Test
{
    ///


    /// TriggerTset.xaml에 대한 상호 작용 논리
    ///

    public partial class TriggerTset : Window
    {
        StackPanel stapan = new StackPanel();

        DoubleAnimation ScaleZoominx = new DoubleAnimation();
        DoubleAnimation ScaleZoominy = new DoubleAnimation();

        DoubleAnimation ScaleZoomoutx = new DoubleAnimation();
        DoubleAnimation ScaleZoomouty = new DoubleAnimation();


        Storyboard str = new Storyboard();
        Storyboard str2 = new Storyboard();
     
        Button btn;

        public TriggerTset()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            stapan.RenderSize = this.RenderSize;

            stapan.Background = new SolidColorBrush(Colors.Black);
            this.Content = stapan;

            stapan.Orientation = Orientation.Horizontal;
            stapan.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

            for (int i = 0; i < 10; i++)
            {
                btn = new Button();
                btn.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                btn.Width = 25;
                btn.Height = 20;
                btn.Name = "test" + i.ToString();
                btn.Content = i.ToString();
                btn.LayoutTransform = new ScaleTransform();
          
                stapan.Children.Add(btn);
            }

            ScaleZoominx.To = 2;
            ScaleZoominx.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            ScaleZoominy.To = 2;
            ScaleZoominy.Duration = new Duration(TimeSpan.FromSeconds(0.25));

            ScaleZoomoutx.To = 1;
            ScaleZoomoutx.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            ScaleZoomouty.To = 1;
            ScaleZoomouty.Duration = new Duration(TimeSpan.FromSeconds(0.25));

            // 프로퍼트에 없을 경우에는 해당하는 함수를 String으로 적어 줘야 함!!
            Storyboard.SetTargetProperty(ScaleZoominx, new PropertyPath("LayoutTransform.ScaleX"));
            Storyboard.SetTargetProperty(ScaleZoominy, new PropertyPath("LayoutTransform.ScaleY"));

            Storyboard.SetTargetProperty(ScaleZoomoutx, new PropertyPath("LayoutTransform.ScaleX"));
            Storyboard.SetTargetProperty(ScaleZoomouty, new PropertyPath("LayoutTransform.ScaleY"));

            str.Children.Add(ScaleZoominx);
            str.Children.Add(ScaleZoominy);

            str2.Children.Add(ScaleZoomoutx);
            str2.Children.Add(ScaleZoomouty);

            BeginStoryboard myBeginStoryboard = new BeginStoryboard();
            myBeginStoryboard.Storyboard = str;

            BeginStoryboard myBeginStoryboard2 = new BeginStoryboard();
            myBeginStoryboard2.Storyboard = str2;


            EventTrigger myMouseEnterTrigger = new EventTrigger();
            myMouseEnterTrigger.RoutedEvent = Button.MouseEnterEvent;
            myMouseEnterTrigger.Actions.Add(myBeginStoryboard);


            EventTrigger myMouseEnterTrigger2 = new EventTrigger();
            myMouseEnterTrigger2.RoutedEvent = Button.MouseLeaveEvent;
            myMouseEnterTrigger2.Actions.Add(myBeginStoryboard2);


            foreach (UIElement ui in stapan.Children)
            {
                Button test = (Button)ui;
                test.Triggers.Add(myMouseEnterTrigger);
                test.Triggers.Add(myMouseEnterTrigger2);
                test.Click += new RoutedEventHandler(test_Click);
            }


           


        }

        void test_Click(object sender, RoutedEventArgs e)
        {
            Button tt = (Button)e.Source; // 발생시킨 개체에 대한 참조값을 읽어 온다!!!
            if (tt.Name == "test1")
            {
            }
           
        }

      
    }
}



해당 내용은 (주)데브존의 웹봇이 자동으로 저희 홈페이지와 API로 동기화한 내용입니다. 자세한 내용은 홈페이지 http://www.devzone.co.kr 또는 모바일 폰에서 http://devzone.co.kr을 접속 하여 확인해 보시기 비랍니다.
• 대표 전화 : 02) 2061 - 0753     • Fax : 02) 2061 - 0759
• 주소 : 서울특별시 양천구 목1동 923-14. 현대드림타워 1024호
• 기술 문의: 02) 2061-1259 김민석 (dev@devzone.co.kr)

댓글 없음:

댓글 쓰기