溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Button控件的動(dòng)畫播放循環(huán)

發(fā)布時(shí)間:2024-08-31 09:03:47 來源:億速云 閱讀:85 作者:小樊 欄目:移動(dòng)開發(fā)

要在Button控件上播放動(dòng)畫并使其循環(huán)播放,你可以使用C#和WPF(Windows Presentation Foundation)編寫一個(gè)簡(jiǎn)單的應(yīng)用程序

  1. 首先,創(chuàng)建一個(gè)新的WPF應(yīng)用程序項(xiàng)目。

  2. 在MainWindow.xaml中添加一個(gè)Button控件:

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Loop Animation Button" Height="150" Width="300">
    <Grid>
       <Button Name="myButton" Content="Click me!" HorizontalAlignment="Center" VerticalAlignment="Center" Click="myButton_Click"/>
    </Grid>
</Window>
  1. 在MainWindow.xaml.cs中添加一個(gè)Storyboard動(dòng)畫,并在Button的Click事件中啟動(dòng)動(dòng)畫:
using System.Windows;
using System.Windows.Media.Animation;

namespace LoopAnimationButton
{
    public partial class MainWindow : Window
    {
        private Storyboard _storyboard;

        public MainWindow()
        {
            InitializeComponent();

            // 創(chuàng)建一個(gè)Storyboard動(dòng)畫
            _storyboard = new Storyboard();

            // 創(chuàng)建一個(gè)DoubleAnimation,用于改變Button的Width屬性
            DoubleAnimation widthAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromSeconds(1)));
            widthAnimation.AutoReverse = true; // 設(shè)置動(dòng)畫自動(dòng)反轉(zhuǎn)
            widthAnimation.RepeatBehavior = RepeatBehavior.Forever; // 設(shè)置動(dòng)畫無限循環(huán)

            // 將動(dòng)畫應(yīng)用于Button的Width屬性
            Storyboard.SetTarget(widthAnimation, myButton);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Button.WidthProperty));

            // 將動(dòng)畫添加到Storyboard
            _storyboard.Children.Add(widthAnimation);
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            // 在Button點(diǎn)擊時(shí)開始播放動(dòng)畫
            _storyboard.Begin();
        }
    }
}

現(xiàn)在,當(dāng)你運(yùn)行這個(gè)應(yīng)用程序并點(diǎn)擊按鈕時(shí),按鈕的寬度將在100和200之間循環(huán)變化。你可以根據(jù)需要修改動(dòng)畫的屬性和持續(xù)時(shí)間。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI