1.Rotation用于3D翻转旋转
目录
- 核心属性
- 1. origin (点坐标)
- 2. axis (旋转轴)
- 3. angle (旋转角度)
- 进阶用法
- 动态绑定示例
- 3D旋转组合
- 特殊注意事项
- 性能考虑:
- 与其它变换的组合:
- 动画应用:
- 应用场景
- 1.横向卡片展示效果
Rotation 是 QML 中用于实现元素旋转的变换类型,属于 Transform 的子类。
以下是其属性的详细解释:
核心属性
1. origin (点坐标)
类型: point (包含 x 和 y 属性)
描述: 定义旋转的中心点
默认值: (0, 0) (元素的左上角)
示例:
origin.x: width / 2 // 水平中心
origin.y: height / 2 // 垂直中心
2. axis (旋转轴)
类型: vector3d (包含 x, y, z 分量)
描述: 定义旋转围绕的3D轴向量
默认值: { x: 0, y: 0, z: 1 } (Z轴,即2D旋转)
常见配置:
2D旋转: axis { z: 1 } (默认)
水平翻转: axis { y: 1 } (类似翻书)
垂直翻转: axis { x: 1 } (类似翻牌)
3. angle (旋转角度)
类型: real (浮点数)
描述: 旋转的角度值,单位为度(°)
范围: 任意值(通常0-360,但可以超过)
正值:顺时针旋转
负值:逆时针旋转
示例:
angle: 45 // 顺时针旋转45度
angle: -90 // 逆时针旋转90度
angle: 720 // 旋转两圈(720度)
进阶用法
动态绑定示例
Rectangle {width: 100; height: 100color: "lightblue"transform: Rotation {origin.x: 50; origin.y: 50axis { x: 0; y: 1; z: 0 }angle: slider.value // 绑定到滑块值}Slider { id: slider; from: 0; to: 360 }
}
3D旋转组合
transform: [Rotation {origin.x: width/2; origin.y: height/2axis { x: 1; y: 0; z: 0 } // X轴旋转angle: xRotation},Rotation {origin.x: width/2; origin.y: height/2axis { x: 0; y: 1; z: 0 } // Y轴旋转angle: yRotation}
]
特殊注意事项
性能考虑:
3D旋转(x或y轴)比2D旋转(z轴)更消耗性能
过多旋转元素可能影响渲染性能
视觉效果:
纯Rotation没有透视效果,如需真实3D效果需要配合:
// 在根元素添加透视
perspective: 400 // 透视值
transformOrigin: Item.Center
与其它变换的组合:
变换顺序会影响最终效果(矩阵乘法不可交换)
通常顺序:缩放(Scale) → 旋转(Rotation) → 平移(Translate)
动画应用:
Rotation {id: rotorigin.x: 50; origin.y: 50angle: 0
}NumberAnimation {target: rotproperty: "angle"from: 0; to: 360duration: 2000loops: Animation.Infinite
}
通过合理配置这些属性,可以实现从简单的2D旋转到复杂的3D翻转等各种视觉效果。
应用场景
1.横向卡片展示效果
结合动画与ListView实现可以悬浮进行阅读堆叠起来的卡片
堆叠
阅读
ListView{anchors.fill: parentorientation: ListView.Horizontalspacing: -50model: 5delegate: Item{id: rootItemwidth: hoverArea.containsMouse? 160:120;height: 100Behavior on width {NumberAnimation{ duration: 200 }}MouseArea{id:hoverAreaanchors.fill: parenthoverEnabled: true}Rectangle {width: 120;height: 100border.color: "#808080"radius: 5antialiasing: truetransform: Rotation {origin.x: rootItem.width * 0.5;origin.y: rootItem.height * 0.5;angle: hoverArea.containsMouse? 0:45axis { x: 0; y: 1; z: 0 }Behavior on angle {NumberAnimation{ duration: 200 }}}Label{text: "Hello World"anchors.fill: parentanchors.margins: 10font.bold: true}}}}