8
我正在创建一个时钟计时器,可以通过拖动时钟手来设置时间(使用触摸输入或鼠标)。为了这个目的,我已经定义了一个AnalogClockHand元件如以下所示,如何通过拖动来旋转qml矩形?
AnalogClockHand.qml
Rectangle {
id: hand
property alias rotationAngle: handRotation.angle
x: (parent.width/2) - (width/2); y: (parent.height/2) - height; z: 2
width: units.gu(1); height: units.gu(14);
radius: units.gu(1)
color: Constants.coolGrey
antialiasing: true
transform: Rotation {
id: handRotation
origin { x: hand.width/2; y: hand.height }
Behavior on angle {
SpringAnimation { spring: 2; damping: 0.3; modulus: 360 }
}
}
}
在主QML文件,我用这个元件和鼠标区域添加到它如以下所示,
主文件设为Qml
AnalogClockHand {
id: secondHand
z: parent.z - 1;
height: units.gu(17); width: units.gu(0.5)
rotationAngle: seconds * 6;
antialiasing: true;
// Implements touch support
MouseArea {
id: timerbackmousearea
property real truex: mouseX - parent.width/2
property real truey: parent.height/2 - mouseY
property real angle: Math.atan2(truex, truey)
property real strictangle: parseInt(angle * 180/Math.PI)
property real modulo: strictangle % 6
anchors.fill: parent
preventStealing: true
onPositionChanged: if (timerbackmousearea.angle < 0) {
secondHand.rotationAngle = strictangle - modulo + 360;
timerValue = parseInt(secondHand.rotationAngle/6);
seconds = timerValue;
}
else {
secondHand.rotationAngle = strictangle - modulo + 6;
timerValue = parseInt(secondHand.rotationAngle/6);
seconds = timerValue;
}
}
}
这种逻辑是但是不能正常工作,而且非常脆弱。所以不能把时间设定得很顺利。在这一天结束时,我试图实现如下图所示的内容。用户应该能够移动小时,分钟或秒针来设定时间。
有没有更好的代码逻辑,我可以实现?
注意1:我意识到图像中的秒针非常小,但是应该尽快修复。