qt qml StackView 触摸滑动返回(swiping pop)

模仿ios的效果
点击item触发push 第二层界面通过动画从右边往左边弹出
然后在最左边向右滑动返回上一层

StackView {
        id: stackView
        anchors.fill: parent
        initialItem: mainView

        property real hideX: 0 - width * 0.3

        pushEnter: Transition {
            XAnimator {
                from: width
                to: 0
                duration: 200
                easing.type: Easing.OutCurve
            }
        }
        pushExit: Transition {
            XAnimator {
                from: 0
                to: stackView.hideX
                duration: 200
                easing.type: Easing.OutCurve
            }
        }
        popEnter: Transition {
            XAnimator {
                from: stackViewMouseArea.prevItem === null ?
                        stackView.hideX : stackViewMouseArea.prevItem.x
                to: 0
                duration: 200
                easing.type: Easing.OutCurve
            }
        }
        popExit: Transition {
            XAnimator {
                from: currentItem.x
                to: width
                duration: 200
                easing.type: Easing.OutCurve
            }
        }
    }

    MouseArea {
        id: stackViewMouseArea
        anchors.fill: parent

        enabled: stackView.depth > 1
        scrollGestureEnabled: false
//        propagateComposedEvents: true

        property bool pressValid: false
        property bool moveValid: false
        property real pressedX
        property real currentX
        property var prevItem: null

        onPressed: {
            if( mouseX < 10
                && mouseY > 60
                && stackView.depth > 1 )
            {
                pressValid = true;
                pressedX = mouseX
                currentX = stackView.currentItem.x
                prevItem = stackView.get(0)
            }
            else {
                mouse.accepted = false
            }
        }
        onPositionChanged: {
            if( pressValid && !moveValid ) {
                if( mouseX - pressedX > 20 ) {
                    moveValid = true
                }
            }
            if( moveValid ) {
                prevItem.visible = true
                var x = mouseX - pressedX + currentX
                if( x > stackView.currentItem.width ) {
                    x = width
                }
                if( x < 0 ) {
                    x = 0
                }

                var p = stackView.currentItem.x / stackView.currentItem.width
                stackView.currentItem.x = x
                prevItem.x = stackView.hideX - (stackView.hideX * p)
            }

        }
        onReleased: {
            if( pressValid ) {
                pressValid = false
                if( moveValid ) {
                    moveValid = false
                    stackView.pop()
                }
            }
        }
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值