在Swift中分别拖动多个图像
我刚刚开始编码,这是我的第一个项目,我想分别拖动屏幕上设置的夫妇图像,然后将它们留在拖动的位置。 但是目前,我可以分别管理拖动图像,但要恢复其原始位置……如果我想将每个图像放在拖动的位置,该怎么办。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来,一旦
贴纸
不再是selected.sticker
,其偏移将返回为零。您需要设置一个变量,该变量在更新后保存图像的位置。您也许可以将另一个变量,
offset
,贴纸
:然后在
sticker.offset.offset.offset.offset.offset
中存储在中.goned
:It seems that once the
sticker
is no longer theselected.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
, toSticker
:And then store the offset value for each sticker in
sticker.offset
in.onEnded
: