缩放背景时 Swiftui 大小图像保持不变

发布于 2025-01-11 17:27:51 字数 2012 浏览 3 评论 0原文

我正在使用以下存储库的一部分: https://github.com/pd95/CS193p-EmojiArt 修改了一些代码,因为我使用图像而不是表情符号,但尺寸部分我无法弄清楚。

表情符号使用相同数字的宽度和高度,但对于我的图像,我使用不同的宽度和高度(所有图像具有相同的宽度和高度)。

当我缩放页面时,图像不会调整大小。

从提到的存储库中,我没有更改大小部分和缩放部分。

有人知道我该如何解决这个问题吗?

使用示例代码更新

大小设置为int:

var size: Int

有一个scaleInstruments函数:

func scaleInstrument(_ instrument: StageManager.Instrument, by scale: CGFloat) {
    if let index = stageManager.instruments.firstIndex(matching: instrument) {
        stageManager.instruments[index].size = Int((CGFloat(stageManager.instruments[index].size) * scale).rounded(.toNearestOrEven))
    }
}

以及zoomScale/zoomGesture函数:

@GestureState private var gestureZoomScale: CGFloat = 1.0

private var zoomScale: CGFloat {
    document.steadyStateZoomScale * (hasSelection ? 1 : gestureZoomScale)
}

private func zoomScale(for instrument: StageManager.Instrument) -> CGFloat {
    if isInstrumentSelected(instrument) {
        return document.steadyStateZoomScale * gestureZoomScale
    }
    else {
        return zoomScale
    }
}

private func zoomGesture() -> some Gesture {
    MagnificationGesture()
        .updating($gestureZoomScale, body: { (latestGestureScale, gestureZoomScale, transaction) in
            gestureZoomScale = latestGestureScale
        })
        .onEnded { finalGestureScale in
            if self.hasSelection {
                self.selectedInstrumentIDs.forEach { (instrumentId) in
                    if let instrument = self.document.instruments.first(where: {$0.id == instrumentId }) {
                        self.document.scaleInstrument(instrument, by: finalGestureScale)
                    }
                }
            }
            else {
                self.document.steadyStateZoomScale *= finalGestureScale
            }
        }
}

希望这足以解释我遇到的问题。

I am using part of the following repo: https://github.com/pd95/CS193p-EmojiArt
Have modified some of the code, as I am using images instead of Emojies, but the size part I can't figure out.

The Emojies use size with a same number for width and height, but with my images, I use a different for the width and height (all images have the same width and height).

When I zoom the page, the images do not resize.

From the mentioned repo, I haven't changed the size part and zoom part.

Somebody has an idea how I can fix that?

Updating with example code

Size is set as int:

var size: Int

There is a scaleInstruments function:

func scaleInstrument(_ instrument: StageManager.Instrument, by scale: CGFloat) {
    if let index = stageManager.instruments.firstIndex(matching: instrument) {
        stageManager.instruments[index].size = Int((CGFloat(stageManager.instruments[index].size) * scale).rounded(.toNearestOrEven))
    }
}

And the zoomScale / zoomGesture functions:

@GestureState private var gestureZoomScale: CGFloat = 1.0

private var zoomScale: CGFloat {
    document.steadyStateZoomScale * (hasSelection ? 1 : gestureZoomScale)
}

private func zoomScale(for instrument: StageManager.Instrument) -> CGFloat {
    if isInstrumentSelected(instrument) {
        return document.steadyStateZoomScale * gestureZoomScale
    }
    else {
        return zoomScale
    }
}

private func zoomGesture() -> some Gesture {
    MagnificationGesture()
        .updating($gestureZoomScale, body: { (latestGestureScale, gestureZoomScale, transaction) in
            gestureZoomScale = latestGestureScale
        })
        .onEnded { finalGestureScale in
            if self.hasSelection {
                self.selectedInstrumentIDs.forEach { (instrumentId) in
                    if let instrument = self.document.instruments.first(where: {$0.id == instrumentId }) {
                        self.document.scaleInstrument(instrument, by: finalGestureScale)
                    }
                }
            }
            else {
                self.document.steadyStateZoomScale *= finalGestureScale
            }
        }
}

Hope this sufficient to explain the issue I have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2025-01-18 17:27:51

我设法做到了这一点,使用以下代码更改:

从:

.frame(width: 140, height: 70)

到:

.frame(width: 140 * self.zoomScale(for: instrument), height: 70 * self.zoomScale(for: instrument))

现在图像将根据缩放调整大小。

I managed to do this, using the following code change:

From:

.frame(width: 140, height: 70)

To:

.frame(width: 140 * self.zoomScale(for: instrument), height: 70 * self.zoomScale(for: instrument))

Now the images will resize according the zoom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文