WPF Marquee Text Animation

后端 未结 4 1139
醉梦人生
醉梦人生 2020-11-27 18:01

I can scroll text with TranslateTransform but when the animation is close to finishing I\'d like it to begin again. Like a snake :)

This is what I\'ve g

4条回答
  •  独厮守ぢ
    2020-11-27 18:31

    The code in above answer does not produce continuous scroll. Here is the code for continuous smooth scroll.

    XAML:

    
        
            
                
                
               
        
    
    

    VB Code Behind:

    Imports System.Windows.Media.Animation
    
    Public Enum Texts
        BoxOne
        BoxTwo
    End Enum
    
    Class Window1
        Private dubAnim As New DoubleAnimation()
        Private dubAnim2 As New DoubleAnimation()
        Private NewsTimer As New Windows.Threading.DispatcherTimer()
        Dim leadText As Texts = Texts.BoxOne
    
        Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            dubAnim.From = ViewingBox.ActualWidth
            dubAnim.To = -BoxOne.ActualWidth
            dubAnim.SpeedRatio = 0.05
            AddHandler dubAnim.Completed, AddressOf dubAnim_Completed
            Timeline.SetDesiredFrameRate(dubAnim, 320)
            BoxOne.BeginAnimation(Canvas.LeftProperty, dubAnim)
    
            dubAnim2.From = ViewingBox.ActualWidth
            dubAnim2.To = -BoxTwo.ActualWidth
            dubAnim2.SpeedRatio = 0.05
            Timeline.SetDesiredFrameRate(dubAnim2, 320)
            AddHandler dubAnim2.Completed, AddressOf dubAnim2_Completed
    
            AddHandler NewsTimer.Tick, AddressOf NewsTimer_Tick
            NewsTimer.Interval = New TimeSpan(0, 0, 0.9)
            NewsTimer.Start()
        End Sub
    
        Private Sub NewsTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            Dim BoxOneLocation As Point = BoxOne.TranslatePoint(New Point(0, 0), ViewingBox)
            Dim BoxTwoLocation As Point = BoxTwo.TranslatePoint(New Point(0, 0), ViewingBox)
    
            If leadText = Texts.BoxOne Then
                Dim loc As Double = BoxOneLocation.X + BoxOne.ActualWidth
                If loc < ViewingBox.ActualWidth / 1.5 Then
                    BoxTwo.BeginAnimation(Canvas.LeftProperty, dubAnim2)
                    NewsTimer.Stop()
                End If
            Else
                Dim loc As Double = BoxTwoLocation.X + BoxTwo.ActualWidth
                If loc < ViewingBox.ActualWidth / 1.5 Then
                    BoxOne.BeginAnimation(Canvas.LeftProperty, dubAnim)
                    NewsTimer.Stop()
                End If
            End If
        End Sub
    
        Private Sub dubAnim_Completed(ByVal sender As Object, ByVal e As EventArgs)
            leadText = Texts.BoxTwo
            NewsTimer.Start()
        End Sub
    
        Private Sub dubAnim2_Completed(ByVal sender As Object, ByVal e As EventArgs)
            leadText = Texts.BoxOne
            NewsTimer.Start()
        End Sub
    End Class
    

提交回复
热议问题