mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-12-13 04:12:03 +08:00
抽象出控件动画类
This commit is contained in:
@@ -18,17 +18,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
initConfig();
|
||||
moveToCenter(this); //让窗口居中显示
|
||||
|
||||
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
|
||||
//设置动画效果
|
||||
animation->setEasingCurve(QEasingCurve::Linear);
|
||||
//设置动画时间(单位:毫秒)
|
||||
animation->setDuration(500);
|
||||
// 设置动画步长值,以及在该位置时显示的透明度
|
||||
animation->setKeyValueAt(0, 0);
|
||||
// m_animation->setKeyValueAt(0.5, 0);
|
||||
animation->setKeyValueAt(1, 1);
|
||||
// 开始动画
|
||||
animation->start();
|
||||
WidgetAnimation::widgetOpacity(this,true);
|
||||
|
||||
downloadlistwidget = new DownloadListWidget;
|
||||
downloadButton = new QPushButton(ui->titlebar);
|
||||
@@ -112,7 +102,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
|
||||
//appintopage按下下载按钮时标题栏下载列表按钮抖动
|
||||
connect(ui->appintopage, &AppIntoPage::clickedDownloadBtn, [=]() {
|
||||
widgetShake(downloadButton,6);//第一个参数是抖动的控件,第二个参数是抖动范围(像素)
|
||||
WidgetAnimation::widgetShake(downloadButton,6);//第一个参数是抖动的控件,第二个参数是抖动范围(像素)
|
||||
});
|
||||
|
||||
connect(backButtom, &QPushButton::clicked, [=]() {
|
||||
@@ -190,26 +180,6 @@ MainWindow::~MainWindow()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::widgetShake(QWidget *pWidget, int nRange)
|
||||
{
|
||||
int nX = pWidget->x();
|
||||
int nY = pWidget->y();
|
||||
QPropertyAnimation *pAnimation = new QPropertyAnimation(pWidget,"geometry");
|
||||
pAnimation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
pAnimation->setDuration(400);
|
||||
pAnimation->setStartValue(QRect(QPoint(nX,nY),pWidget->size()));
|
||||
|
||||
int nShakeCount = 8;
|
||||
double nStep = 1.0/nShakeCount;
|
||||
for(int i = 1; i < nShakeCount; i++){
|
||||
nRange = i&1 ? -nRange : nRange;
|
||||
pAnimation->setKeyValueAt(nStep*i,QRect(QPoint(nX + nRange,nY),pWidget->size()));
|
||||
}
|
||||
|
||||
pAnimation->setEndValue(QRect(QPoint(nX,nY),pWidget->size()));
|
||||
pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
|
||||
}
|
||||
|
||||
void MainWindow::openUrl(QUrl url)
|
||||
{
|
||||
if (url.toString().startsWith("spk://")) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <QDesktopServices>
|
||||
|
||||
#include "widgets/downloadlistwidget.h"
|
||||
#include "utils/widgetanimation.h"
|
||||
|
||||
DWIDGET_USE_NAMESPACE
|
||||
|
||||
@@ -31,7 +32,6 @@ public:
|
||||
void openUrl(QUrl);
|
||||
|
||||
private:
|
||||
void widgetShake(QWidget *pWidget, int nRange);
|
||||
void initConfig();
|
||||
void switchPage(int now);
|
||||
void updateUi(int now);
|
||||
|
||||
@@ -35,6 +35,7 @@ SOURCES += \
|
||||
mainwindow-dtk.cpp \
|
||||
backend/sparkapi.cpp \
|
||||
pages/settingspage.cpp \
|
||||
utils/widgetanimation.cpp \
|
||||
widgets/common/webenginepage.cpp \
|
||||
widgets/common/webengineview.cpp \
|
||||
widgets/downloadlistwidget.cpp \
|
||||
@@ -52,6 +53,7 @@ HEADERS += \
|
||||
mainwindow-dtk.h \
|
||||
backend/sparkapi.h \
|
||||
pages/settingspage.h \
|
||||
utils/widgetanimation.h \
|
||||
widgets/common/webenginepage.h \
|
||||
widgets/common/webengineview.h \
|
||||
widgets/downloadlistwidget.h \
|
||||
|
||||
47
src/utils/widgetanimation.cpp
Normal file
47
src/utils/widgetanimation.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "widgetanimation.h"
|
||||
|
||||
WidgetAnimation::WidgetAnimation()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WidgetAnimation::widgetShake(QWidget *pWidget, int nRange)
|
||||
{
|
||||
int nX = pWidget->x();
|
||||
int nY = pWidget->y();
|
||||
QPropertyAnimation *pAnimation = new QPropertyAnimation(pWidget,"geometry");
|
||||
pAnimation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
pAnimation->setDuration(400);
|
||||
pAnimation->setStartValue(QRect(QPoint(nX,nY),pWidget->size()));
|
||||
|
||||
int nShakeCount = 8;
|
||||
double nStep = 1.0/nShakeCount;
|
||||
for(int i = 1; i < nShakeCount; i++){
|
||||
nRange = i&1 ? -nRange : nRange;
|
||||
pAnimation->setKeyValueAt(nStep*i,QRect(QPoint(nX + nRange,nY),pWidget->size()));
|
||||
}
|
||||
|
||||
pAnimation->setEndValue(QRect(QPoint(nX,nY),pWidget->size()));
|
||||
pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
|
||||
}
|
||||
|
||||
void WidgetAnimation::widgetOpacity(QWidget *pWidget, bool isAppear)
|
||||
{
|
||||
QPropertyAnimation *animation = new QPropertyAnimation(pWidget, "windowOpacity", pWidget);
|
||||
//设置动画效果
|
||||
animation->setEasingCurve(QEasingCurve::Linear);
|
||||
//设置动画时间(单位:毫秒)
|
||||
animation->setDuration(500);
|
||||
// 设置动画步长值,以及在该位置时显示的透明度
|
||||
if(isAppear)
|
||||
{
|
||||
animation->setKeyValueAt(0, 0);
|
||||
// m_animation->setKeyValueAt(0.5, 0);
|
||||
animation->setKeyValueAt(1, 1);
|
||||
}else{
|
||||
animation->setKeyValueAt(0, 1);
|
||||
animation->setKeyValueAt(1, 0);
|
||||
}
|
||||
// 开始动画
|
||||
animation->start();
|
||||
}
|
||||
16
src/utils/widgetanimation.h
Normal file
16
src/utils/widgetanimation.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef WIDGETANIMATION_H
|
||||
#define WIDGETANIMATION_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
#include <QPropertyAnimation>
|
||||
|
||||
class WidgetAnimation
|
||||
{
|
||||
public:
|
||||
WidgetAnimation();
|
||||
static void widgetShake(QWidget *pWidget, int nRange);
|
||||
static void widgetOpacity(QWidget *pWidget, bool isAppear);
|
||||
};
|
||||
|
||||
#endif // WIDGETANIMATION_H
|
||||
Reference in New Issue
Block a user