在Swift中分别拖动多个图像

发布于 2025-02-09 12:37:49 字数 928 浏览 2 评论 0原文

我刚刚开始编码,这是我的第一个项目,我想分别拖动屏幕上设置的夫妇图像,然后将它们留在拖动的位置。 但是目前,我可以分别管理拖动图像,但要恢复其原始位置……如果我想将每个图像放在拖动的位置,该怎么办。

HStack {
    ForEach(stickers) { sticker in
        Image(sticker.name)
            .resizable()
            .frame(width: self.screenWidth*0.2, height: self.screenWidth*0.2)
            .offset(x: self.selectedSticker == sticker ? self.dragOffset.width : 0,
                    y: self.selectedSticker == sticker ? self.dragOffset.height : 0)
            .gesture(
                DragGesture()
                    .updating(self.$dragOffset, body: { (value, state, transaction) in
                        if nil == self.selectedSticker {
                            self.selectedSticker = sticker
                        }
                        state = value.translation
                        
                    }).onEnded { _ in self.selectedSticker = nil }
            )
    }
}        

I have just started coding and this is my first project, and I want to drag couple images set on the screen separately and leave them where they are dragged.
However currently I could manage dragging images separately but comes back in their original position... what should I change if I want to leave each images where they are dragged.

HStack {
    ForEach(stickers) { sticker in
        Image(sticker.name)
            .resizable()
            .frame(width: self.screenWidth*0.2, height: self.screenWidth*0.2)
            .offset(x: self.selectedSticker == sticker ? self.dragOffset.width : 0,
                    y: self.selectedSticker == sticker ? self.dragOffset.height : 0)
            .gesture(
                DragGesture()
                    .updating(self.$dragOffset, body: { (value, state, transaction) in
                        if nil == self.selectedSticker {
                            self.selectedSticker = sticker
                        }
                        state = value.translation
                        
                    }).onEnded { _ in self.selectedSticker = nil }
            )
    }
}        

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

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

发布评论

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

评论(1

落花浅忆 2025-02-16 12:37:49

看来,一旦贴纸不再是selected.sticker,其偏移将返回为零。您需要设置一个变量,该变量在更新后保存图像的位置。

您也许可以将另一个变量,offset贴纸

struct Sticker: ... {
    var offset = CGSize.zero
    ...
}

然后在sticker.offset.offset.offset.offset.offset中存储在中.goned

HStack {
    ForEach(stickers) { sticker in
        Image(sticker.name)
            .resizable()
            .frame(width: self.screenWidth*0.2, height: self.screenWidth*0.2)
            .offset(x: self.selectedSticker == sticker ? self.dragOffset.width : sticker.offset.width,
                    y: self.selectedSticker == sticker ? self.dragOffset.height : sticker.offset.height)
            .gesture(
                DragGesture()
                    .updating(self.$dragOffset, body: { (value, state, transaction) in
                        if nil == self.selectedSticker {
                            self.selectedSticker = sticker
                        }
                        state = value.translation
                    })
                    .onEnded { _ in
                        sticker.offset = dragOffset
                        self.selectedSticker = nil
                    }
            )
    }
}    

It seems that once the sticker is no longer the selected.sticker, its offset returns to zero. You'd need to set a variable that saves the images' locations after they are updated.

You could perhaps add another variable, offset, to Sticker:

struct Sticker: ... {
    var offset = CGSize.zero
    ...
}

And then store the offset value for each sticker in sticker.offset in .onEnded:

HStack {
    ForEach(stickers) { sticker in
        Image(sticker.name)
            .resizable()
            .frame(width: self.screenWidth*0.2, height: self.screenWidth*0.2)
            .offset(x: self.selectedSticker == sticker ? self.dragOffset.width : sticker.offset.width,
                    y: self.selectedSticker == sticker ? self.dragOffset.height : sticker.offset.height)
            .gesture(
                DragGesture()
                    .updating(self.$dragOffset, body: { (value, state, transaction) in
                        if nil == self.selectedSticker {
                            self.selectedSticker = sticker
                        }
                        state = value.translation
                    })
                    .onEnded { _ in
                        sticker.offset = dragOffset
                        self.selectedSticker = nil
                    }
            )
    }
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文