旋转动画 sl
public Rotate() { InitializeComponent(); Rectangle r = new Rectangle(); r.Width = 10; r.Height = 50; r.Fill = new SolidColorBrush(Colors.Red); Canvas.SetLeft(r, 100); Canvas.SetTop(r, 100); RotateTransform rt = new RotateTransform(); rt.CenterX = 0; rt.CenterY = 0; rt.Angle = 90; r.RenderTransform = rt; LayoutRoot.Background = new SolidColorBrush(Colors.Gray); LayoutRoot.Children.Add(r); Rectangle r1 = new Rectangle(); r1.Width = 10; r1.Height = 50; r1.Fill = new SolidColorBrush(Colors.Blue); Canvas.SetLeft(r1, 100); Canvas.SetTop(r1, 100); LayoutRoot.Children.Add(r1); LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(Mouse_Left_Down); } protected void Mouse_Left_Down(object sender, EventArgs e) { Rectangle r=(Rectangle)LayoutRoot.Children[0]; Storyboard sb = new Storyboard(); DoubleAnimation da = new DoubleAnimation(); da.From = ((RotateTransform)(r.RenderTransform)).Angle; da.To = 360; da.Duration = new Duration(TimeSpan.FromMilliseconds(1000)); Storyboard.SetTarget(da, r); //下面的属性路径不知道怎么写,好像就是这里有问题 Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty)); sb.Children.Add(da); sb.Begin(); }
Button myButton; void MainPage_Loaded(object sender, RoutedEventArgs e) { myButton = new Button(); myButton.Height = 25; myButton.Width = 100; myButton.Content = "Click me"; //myButton.RenderTransformOrigin = new Point(10,10); ScaleTransform myScTransform = new ScaleTransform(); TransformGroup myTransGroup = new TransformGroup(); //要在后面的代码中使用必须先进行注册 myTransGroup.Children.Add(myScTransform); myButton.RenderTransform = myTransGroup; myButton.Click += new RoutedEventHandler(myButton_Click); LayoutRoot.Children.Add(myButton); } void myButton_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard(); DoubleAnimation yAnimation = new DoubleAnimation(); yAnimation.From = 0.5; yAnimation.To = 1.0; yAnimation.Duration = new Duration(TimeSpanFrom5)); ScaleTransform transY = (ScaleTransform)((TransformGroup)myButton.RenderTransform).Children[0]; //找到你注册的属性 Storyboard.SetTarget(yAnimation, transY); Storyboard.SetTargetProperty(yAnimation, new PropertyPath(ScaleTransform.ScaleYProperty)); story.Children.Add(yAnimation); story.Begin(); }
[解决办法]
protected void Mouse_Left_Down(object sender, EventArgs e){TransformGroup group = new TransformGroup( );RotateTransform rotate = new RotateTransform( ); //旋转group.Children.Add( rotate );this.btn2.RenderTransform = group;//确定中心点this.btn2.RenderTransformOrigin = new Point( 0.5 , 0.5 );//创建关键帧DoubleAnimationUsingKeyFrames angle = new DoubleAnimationUsingKeyFrames( );//创建各个帧的动画效果SplineDoubleKeyFrame spTranslate = new SplineDoubleKeyFrame( 180 , KeyTime.FromTimeSpan( new TimeSpan( 0 , 0 , 0 , 0 , 800 ) ) , new KeySpline( 0 , 0 , 1 , 1 ) );//将单独的帧插入到关键帧中angle.KeyFrames.Add( spTranslate );//定义故事版Storyboard story = new Storyboard( );//将关键帧放入控件中Storyboard.SetTarget( angle , this.btn2 );//定义动画类型------此处就是你说的路径,children后面的[0]旋转插入到group的位置Storyboard.SetTargetProperty( angle , new PropertyPath( "(Button.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)" ) );//将关键帧加入故事版story.Children.Add( angle );//运行故事版story.Begin( );}