首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置flickable的最大/最小缩放

如何设置flickable的最大/最小缩放
EN

Stack Overflow用户
提问于 2018-09-06 21:49:10
回答 1查看 414关注 0票数 0

我正在尝试制作可缩放的Item (Image或任何其他组件),并将其用于平板电脑。目前,我的代码如下所示:

代码语言:javascript
复制
Rectangle {
    id: root
    anchors.fill: parent
    color: mainWindow.toGradient
    Flickable {
        id: flick
        anchors.fill: parent
        contentWidth: width
        contentHeight: height
        boundsBehavior: Flickable.StopAtBounds

        PinchArea {
            id: pArea
            width: Math.max(flick.contentWidth, flick.width)
            height: Math.max(flick.contentHeight, flick.height)
            property real initialWidth
            property real initialHeight
            onPinchStarted: {
                initialWidth = flick.contentWidth
                initialHeight = flick.contentHeight
            }
            onPinchUpdated: {
                flick.contentX += pinch.previousCenter.x - pinch.center.x
                flick.contentY += pinch.previousCenter.y - pinch.center.y

                flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center)
            }

            onPinchFinished: {
                flick.returnToBounds()
            }

            Rectangle {
                width: flick.contentWidth
                height: flick.contentHeight
                color: "black"
                Image {
                    anchors.fill: parent
                    source: "qrc:/image.jpg"
                }

                MouseArea {
                    anchors.fill: parent
                    onDoubleClicked: {
                        flick.contentWidth = root.width
                        flick.contentHeight = root.height
                    }
                }
            }
        }
    }
}

它工作得很好,但当它在整个屏幕上时,我仍然可以缩小(缩小它,即父对象)。

我如何才能在某个点/比例停止缩小(如果可能的话,还可以停止放大)?

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-11 22:49:35

最后,我删除了Flickable并扩展了Item

代码语言:javascript
复制
Rectangle {
    id: root
    anchors.fill: parent
    color: "black"

    Item {
        id: photoFrame
        width: root.width
        height: root.height
        Image { 
            id: image
            anchors.fill: parent
            source: "image.jpg"
        }

        PinchArea {
            anchors.fill: parent
            pinch.target: photoFrame
            pinch.minimumRotation: 0 //Rotation (i guess we don't need to rotate it)
            pinch.maximumRotation: 0
            pinch.minimumScale: 1 //Minimum zoom for camera
            pinch.maximumScale: 4 //Maximum zoom for camera

            onPinchUpdated: { //Dont zoom behind border
                if(photoFrame.x < dragArea.drag.minimumX)
                    photoFrame.x = dragArea.drag.minimumX
                else if(photoFrame.x > dragArea.drag.maximumX)
                    photoFrame.x = dragArea.drag.maximumX

                if(photoFrame.y < dragArea.drag.minimumY)
                    photoFrame.y = dragArea.drag.minimumY
                else if(photoFrame.y > dragArea.drag.maximumY)
                    photoFrame.y = dragArea.drag.maximumY
            }

            MouseArea {
                id: dragArea
                hoverEnabled: true
                anchors.fill: parent
                drag.target: photoFrame
                scrollGestureEnabled: false  // 2-finger-flick gesture should pass through to the Flickable
                drag.minimumX: (root.width - (photoFrame.width * photoFrame.scale))/2
                drag.maximumX: -(root.width - (photoFrame.width * photoFrame.scale))/2
                drag.minimumY: (root.height - (photoFrame.height * photoFrame.scale))/2
                drag.maximumY: -(root.height - (photoFrame.height * photoFrame.scale))/2

                onDoubleClicked: { //Reset size and location of camera
                    photoFrame.x = 0
                    photoFrame.y = 0
                    photoFrame.scale = 1
                }
                onWheel: {
                    var scaleBefore = photoFrame.scale
                    photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10
                    if(photoFrame.scale < 1)
                        photoFrame.scale = 1
                    else if(photoFrame.scale > 4)
                        photoFrame.scale = 4

                    if(photoFrame.x < drag.minimumX)//Dont zoom behind border of image
                        photoFrame.x = drag.minimumX
                    else if(photoFrame.x > drag.maximumX)
                        photoFrame.x = drag.maximumX

                    if(photoFrame.y < drag.minimumY)
                        photoFrame.y = drag.minimumY
                    else if(photoFrame.y > drag.maximumY)
                        photoFrame.y = drag.maximumY
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52205602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档