State与Transition之State

Item的state和states属性

所有Item都有个state属性,保存着当前状态的名字,默认值是空字符串(默认状态)
所有Item都有个states 属性,保存着当前Item的所有状态,类型是list < State >

State QML Type

Properties

  • changes : list< Change >//状态的所有变化
  • extend : string//指向当前状态基态的名字,基态的所有变化都会被继承
  • name : string//状态名字
  • when : bool //为true,应用该状态(置当前Item的state置为该状态名),为false切换到默认状态。当多个State的when为true时,第一个State生效(变为fasle时切换到下一个State为true的,没有切换到默认状态)。
    应用状态两种方式:1设置Item的state(对应的状态when为false不会生效)2设置when为true(使用when时必须是绑定到表达式上。)

changes : list< Change >

  • PropertyChanges 改变属性
  • AnchorChanges 改变锚
  • ParentChange 改变父项目
  • StateChangeScript 运行脚本
  • StateGroup

PropertyChanges

  import QtQuick 2.0

  Item {
      id: container
      width: 300; height: 300

      Rectangle {
          id: rect
          width: 100; height: 100
          color: "red"

          MouseArea {
             id: mouseArea
             anchors.fill: parent
          }

          states: State {
             name: "resized"; when: mouseArea.pressed
             PropertyChanges { target: rect; color: "blue"; height: container.height }
          }
      }
  }

AnchorChanges

 import QtQuick 2.0

  Rectangle {
      id: window
      width: 120; height: 120
      color: "black"

      Rectangle { id: myRect; width: 50; height: 50; color: "red" }

      states: State {
          name: "reanchored"

          AnchorChanges {
              target: myRect
              anchors.top: window.top
              anchors.bottom: window.bottom
          }
          PropertyChanges {
              target: myRect
              anchors.topMargin: 10
              anchors.bottomMargin: 10
          }
      }

      MouseArea { anchors.fill: parent; onClicked: window.state = "reanchored" }
  }

ParentChange

  import QtQuick 2.0

  Item {
      width: 200; height: 100

      Rectangle {
          id: redRect
          width: 100; height: 100
          color: "red"
      }

      Rectangle {
          id: blueRect
          x: redRect.width
          width: 50; height: 50
          color: "blue"

          states: State {
              name: "reparented"
              ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
          }

          MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
      }
  }

StateChangeScript

  State {
      name: "state1"
      StateChangeScript {
          name: "myScript"
          script: doStateStuff();
      }
      // ...
  }

相关链接
Qt Quick States

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
transition-group是React的一个组件,它用于在一组DOM元素上添加动画效果。它通常与CSSTransition组件一起使用,CSSTransition组件用于定义动画的进入和退出效果。 下面是一个使用transition-group和CSSTransition的例子: ```jsx import React, { Component } from 'react'; import { CSSTransition, TransitionGroup } from 'react-transition-group'; import './style.css'; class App extends Component { constructor(props) { super(props); this.state = { items: ['Item 1', 'Item 2', 'Item 3'] }; } addItem = () => { const newItem = prompt('Enter item name:'); if (newItem) { this.setState(prevState => ({ items: [...prevState.items, newItem] })); } }; removeItem = index => { this.setState(prevState => ({ items: prevState.items.filter((item, i) => i !== index) })); }; render() { const { items } = this.state; return ( <div> <button onClick={this.addItem}>Add Item</button> <TransitionGroup> {items.map((item, index) => ( <CSSTransition key={index} timeout={500} classNames="fade"> <div> {item} <button onClick={() => this.removeItem(index)}>Remove</button> </div> </CSSTransition> ))} </TransitionGroup> </div> ); } } export default App; ``` 在上面的例子中,我们使用了transition-group和CSSTransition来实现一个简单的列表,点击"Add Item"按钮可以添加新的项,每个项都有一个"Remove"按钮可以删除。当添加或删除项时,会有一个淡入淡出的动画效果。 CSS样式文件(style.css)中定义了.fade类的动画效果,你可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值