在最近做前端时,我发现Behavior这个类型意外的好用,相比Animation类,Behavior能够绑定属性,自动管理过渡动画,减少冗余的动画代码,使代码更加简洁,举个例子:
Behavior绑定属性,即只要这个属性有变动,自动播放过渡动画(color的话需要ColorAnimation,NumberAnimation控制的是数值类型,如int、real等)
Rectangle{
width: 100
height: 70
color:"red"
x:100
y:isplay?0:300
property bool isplay: false
MouseArea{
anchors.fill: parent
onClicked: {
parent.isplay=!parent.isplay
}
}
Behavior on y {
NumberAnimation{
duration: 500
easing.type: Easing.OutCubic
}
}
}
而单独的NumberAnimation就相当繁琐冗余了,不仅需要命名来调用函数,还需要仔细地控制每个动画的行为,对比来看就是,下面这串代码能做到与上面相同的效果
Rectangle{
id:rec
width: 100
height: 70
color:"red"
x:100
y:0
property bool isplay: false
MouseArea{
anchors.fill: parent
onClicked: {
parent.isplay=!parent.isplay
if(parent.isplay){
recanim2.start()
}
else{
recanim1.start()
}
}
}
NumberAnimation{
id:recanim1
target: rec
property: "y"
from:300
to:0
duration: 500
easing.type: Easing.OutCubic
}
NumberAnimation{
id:recanim2
target: rec
property: "y"
from:0
to:300
duration: 500
easing.type: Easing.OutCubic
}
}
通过对比,很明显,Behavior在简单的动画上能极大的精简动画代码,但单独的Animation类型能够完成更加复杂的动画,它的属性更多,可控性也更高,能够通过成员函数自主控制动画
所以Behavior是在特定的简单动画上的更优方案,我们可以用来大幅的简化代码,但在复杂可控的动画上,还是需要Animation类型来实现的
565

被折叠的 条评论
为什么被折叠?



