Add animations to controls

Visual Basic tutorials for UWP apps.
1 post Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Add animations to controls
CodenStuff
Various Storyboard codes to add animations to XAML controls

I'm posting these here to share and so I don't lose them if my main system goes down lol

You can apply the animation to any control on the XAML page, example:
Code: Select all
BounceAni(Button1)
Need Import:
Code: Select all
Imports Windows.UI.Xaml.Media.Animation
Bounce:
Code: Select all
    Public Sub BounceAni(sender As Object)
        Dim da As DoubleAnimation = New DoubleAnimation()
        da.Duration = New Duration(TimeSpan.FromMilliseconds(200))
        da.From = 0
        da.To = -20
        da.RepeatBehavior = New RepeatBehavior("1")
        da.AutoReverse = True
        CType(sender, Button).Projection = New PlaneProjection
        da.EnableDependentAnimation = True


        Dim sb As Storyboard = New Storyboard()
        Storyboard.SetTarget(da, CType(sender, Button).Projection)
        Storyboard.SetTargetProperty(da, "GlobalOffsetY")

        sb.Children.Add(da)

        sb.Begin()
    End Sub
Spin:
Code: Select all
    Public Sub SpinAni(sender As Object)
        CType(sender, Button).Visibility = Windows.UI.Xaml.Visibility.Visible
        Dim Scalex As DoubleAnimation = New DoubleAnimation()
        Scalex.Duration = New Duration(TimeSpan.FromMilliseconds(200))
        Scalex.From = 0
        Scalex.To = 360
        'Scalex.AutoReverse = True
        Scalex.RepeatBehavior = New RepeatBehavior("1")
        CType(sender, Button).RenderTransform = New RotateTransform
        Scalex.EnableDependentAnimation = True
        Dim Popout As Storyboard = New Storyboard()
        Storyboard.SetTarget(Scalex, CType(sender, Button).RenderTransform)
        Storyboard.SetTargetProperty(Scalex, "Angle")
        Popout.Children.Add(Scalex)
        Popout.Begin()
    End Sub
Zoom out and in:
Code: Select all
    Public Sub ZoomOutAni(sender As Object)
        Dim Scalex As DoubleAnimation = New DoubleAnimation()
        Scalex.Duration = New Duration(TimeSpan.FromMilliseconds(300))
        Scalex.From = 1
        Scalex.To = 2
        Scalex.AutoReverse = True
        Scalex.RepeatBehavior = New RepeatBehavior("1")
        CType(sender, Button).RenderTransform = New ScaleTransform
        Scalex.EnableDependentAnimation = True

        Dim ScaleY As DoubleAnimation = New DoubleAnimation()
        ScaleY.Duration = New Duration(TimeSpan.FromMilliseconds(300))
        ScaleY.From = 1
        ScaleY.To = 2
        ScaleY.AutoReverse = True
        ScaleY.RepeatBehavior = New RepeatBehavior("1")
        CType(sender, Button).RenderTransform = New ScaleTransform
        ScaleY.EnableDependentAnimation = True

        Dim Popout As Storyboard = New Storyboard()
        Storyboard.SetTarget(Scalex, CType(sender, Button).RenderTransform)
        Storyboard.SetTargetProperty(Scalex, "ScaleX")
        Storyboard.SetTarget(ScaleY, CType(sender, Button).RenderTransform)
        Storyboard.SetTargetProperty(ScaleY, "ScaleY")

        Popout.Children.Add(Scalex)
        Popout.Children.Add(ScaleY)
        Popout.Begin()

    End Sub
Zoom in and out:
Code: Select all
    Public Sub ZoomInAni(ByVal sender As [Object])
        Dim doubleAnimation As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        Dim duration As Windows.UI.Xaml.Duration = New Windows.UI.Xaml.Duration(TimeSpan.FromMilliseconds(300))
        doubleAnimation.Duration = duration
        doubleAnimation.From = 0
        doubleAnimation.To = 1
        doubleAnimation.AutoReverse = False
        Dim repeatBehavior As Windows.UI.Xaml.Media.Animation.RepeatBehavior = New Windows.UI.Xaml.Media.Animation.RepeatBehavior("1")
        doubleAnimation.RepeatBehavior = repeatBehavior
        DirectCast(sender, Button).RenderTransform = New ScaleTransform()
        doubleAnimation.EnableDependentAnimation = True
        Dim doubleAnimation1 As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        duration = New Windows.UI.Xaml.Duration(TimeSpan.FromMilliseconds(300))
        doubleAnimation1.Duration = duration
        doubleAnimation1.From = 0
        doubleAnimation1.To = 1
        doubleAnimation1.AutoReverse = False
        repeatBehavior = New Windows.UI.Xaml.Media.Animation.RepeatBehavior("1")
        doubleAnimation1.RepeatBehavior = repeatBehavior
        DirectCast(sender, Button).RenderTransform = New ScaleTransform()
        doubleAnimation1.EnableDependentAnimation = True
        Dim storyboard As Windows.UI.Xaml.Media.Animation.Storyboard = New Windows.UI.Xaml.Media.Animation.Storyboard()
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation, (DirectCast(sender, Button)).RenderTransform())
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation, "ScaleX")
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation1, (DirectCast(sender, Button)).RenderTransform())
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation1, "ScaleY")
        storyboard.Children().Add(doubleAnimation)
        storyboard.Children().Add(doubleAnimation1)
        storyboard.Begin()
    End Sub
Slide Down:
Code: Select all
    Public Sub SlideDownAni(ByVal sender As [Object])
        DirectCast(sender, Button).Visibility = 0
        Dim doubleAnimation As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        doubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(500))
        doubleAnimation.From = -500
        doubleAnimation.To = 0
        doubleAnimation.RepeatBehavior = New RepeatBehavior("1")
        DirectCast(sender, Button).Projection = New PlaneProjection()
        doubleAnimation.EnableDependentAnimation = True
        Dim storyboard As Windows.UI.Xaml.Media.Animation.Storyboard = New Windows.UI.Xaml.Media.Animation.Storyboard()
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation, (DirectCast(sender, Button)).Projection())
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation, "GlobalOffsetY")
        storyboard.Children().Add(doubleAnimation)
        storyboard.Begin()
    End Sub
Slide from Right:
Code: Select all
    Public Sub SlideRightAni(ByVal sender As [Object])
        DirectCast(sender, Button).Visibility = 0
        Dim doubleAnimation As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        doubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(500))
        doubleAnimation.From = 500
        doubleAnimation.To = 0
        doubleAnimation.RepeatBehavior = New RepeatBehavior("1")
        DirectCast(sender, Button).Projection = New PlaneProjection()
        doubleAnimation.EnableDependentAnimation = True
        Dim storyboard As Windows.UI.Xaml.Media.Animation.Storyboard = New Windows.UI.Xaml.Media.Animation.Storyboard()
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation, (DirectCast(sender, Button).Projection()))
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation, "GlobalOffsetX")
        storyboard.Children().Add(doubleAnimation)
        storyboard.Begin()
    End Sub
Slide from Left:
Code: Select all
    Public Sub SlideLeftAni(ByVal sender As [Object])
        DirectCast(sender, Button).Visibility = 0
        Dim doubleAnimation As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        doubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(500))
        doubleAnimation.From = -500
        doubleAnimation.To = 0
        doubleAnimation.RepeatBehavior = New RepeatBehavior("1")
        DirectCast(sender, Button).Projection = New PlaneProjection()
        doubleAnimation.EnableDependentAnimation = True
        Dim storyboard As Windows.UI.Xaml.Media.Animation.Storyboard = New Windows.UI.Xaml.Media.Animation.Storyboard()
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation, (DirectCast(sender, Button).Projection()))
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation, "GlobalOffsetX")
        storyboard.Children().Add(doubleAnimation)
        storyboard.Begin()
    End Sub
Slide Up:
Code: Select all
    Public Sub SlideUpAni(ByVal sender As [Object])
        DirectCast(sender, Button).Visibility = 0
        Dim doubleAnimation As Windows.UI.Xaml.Media.Animation.DoubleAnimation = New Windows.UI.Xaml.Media.Animation.DoubleAnimation()
        doubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(500))
        doubleAnimation.From = 500
        doubleAnimation.To = 0
        doubleAnimation.RepeatBehavior = New RepeatBehavior("1")
        DirectCast(sender, Button).Projection = New PlaneProjection()
        doubleAnimation.EnableDependentAnimation = True
        Dim storyboard As Windows.UI.Xaml.Media.Animation.Storyboard = New Windows.UI.Xaml.Media.Animation.Storyboard()
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(doubleAnimation, (DirectCast(sender, Button).Projection()))
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(doubleAnimation, "GlobalOffsetY")
        storyboard.Children().Add(doubleAnimation)
        storyboard.Begin()
    End Sub
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
1 post Page 1 of 1
Return to “Visual Basic”