id 标识符 swift
id Identifier swift
我有这些代码行,如果我在 X 上停下来删除图像,我会删除它。问题是,如果我有一个或多个相同类型的图像,删除其中一个,它也会删除所有其他相同的图像。我认为我的问题在于调用 "forEach"。我应该使用什么?
如果图像不同,我会正确删除单个 "sticker"。
guard let id = gestureRecognizer.stringIdentifier else {return}
stickerArray.forEach { (sticker) in
if id == sticker.stickerIdentifier {
STKProbes.deleteSticker.send(from: nil, info: sticker.stickerIdentifier)
sticker.removeFromSuperview()
sticker.image = nil
if let index = stickerArray.index(of: sticker) {
stickerArray.remove(at: index)
}
}
}
func appendSticker(model: CoreSceneViewModel.AddSticker.ViewModel) {
let sticker = StickerView(frame: PlacementManager.categoryPlacement(forCategory: model.category, over: self.managedView.workbenchView), name: model.name)
self.managedView.controlsView.alpha = 0
self.managedView.closeButton.alpha = 0
self.stickerArray.append(sticker)
STKProbes.tapSticker.send(from: nil, info: sticker.stickerIdentifier)
sticker.image = model.sticker
sticker.contentMode = .scaleAspectFit
if kFreezeOldStickers {
for view in self.managedView.workbenchView.subviews {
if let grs = view.gestureRecognizers {
for gr in grs {
view.removeGestureRecognizer(gr)
}
}
}
}
draggingGesture = UIPanGestureRecognizer(target: self, action: #selector(dragging(gestureRecognizer:)))
sticker.addGestureRecognizer(draggingGesture)
pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinched(gestureRecognizer:)))
sticker.addGestureRecognizer(pinchGesture)
rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotated(gestureRecognizer:)))
sticker.addGestureRecognizer(rotationGesture)
tapGestureAlpha = UITapGestureRecognizer(target: self, action: #selector(setAlpha0(gestureRecognizer:)))
self.managedView.workbenchView.addGestureRecognizer(tapGestureAlpha)
tapGesture = TapGesture(target: self, action: #selector(tapped(gestureRecognizer:)))
tapGesture.idIdentifier = sticker.stickerIdentifier
sticker.addGestureRecognizer(tapGesture)
dTapGesture = DeleteTapGesture(target: self, action: #selector(cancelTapped(gestureRecognizer:)))
dTapGesture.stringIdentifier = sticker.stickerIdentifier
sticker.xButton.addGestureRecognizer(dTapGesture)
sticker.xButton.isUserInteractionEnabled = true
sticker.isUserInteractionEnabled = true
sticker.layer.zPosition = 0
draggingGesture.delegate = self
self.managedView.workbenchView.addSubview(sticker)
你可以试试
stickerArray.forEach {
if [=10=].xButton == gestureRecognizer.view {
[=10=].removeFromSuperview()
}
}
您可能有类似的 stringIdentifier
商品
我有这些代码行,如果我在 X 上停下来删除图像,我会删除它。问题是,如果我有一个或多个相同类型的图像,删除其中一个,它也会删除所有其他相同的图像。我认为我的问题在于调用 "forEach"。我应该使用什么?
如果图像不同,我会正确删除单个 "sticker"。
guard let id = gestureRecognizer.stringIdentifier else {return}
stickerArray.forEach { (sticker) in
if id == sticker.stickerIdentifier {
STKProbes.deleteSticker.send(from: nil, info: sticker.stickerIdentifier)
sticker.removeFromSuperview()
sticker.image = nil
if let index = stickerArray.index(of: sticker) {
stickerArray.remove(at: index)
}
}
}
func appendSticker(model: CoreSceneViewModel.AddSticker.ViewModel) {
let sticker = StickerView(frame: PlacementManager.categoryPlacement(forCategory: model.category, over: self.managedView.workbenchView), name: model.name)
self.managedView.controlsView.alpha = 0
self.managedView.closeButton.alpha = 0
self.stickerArray.append(sticker)
STKProbes.tapSticker.send(from: nil, info: sticker.stickerIdentifier)
sticker.image = model.sticker
sticker.contentMode = .scaleAspectFit
if kFreezeOldStickers {
for view in self.managedView.workbenchView.subviews {
if let grs = view.gestureRecognizers {
for gr in grs {
view.removeGestureRecognizer(gr)
}
}
}
}
draggingGesture = UIPanGestureRecognizer(target: self, action: #selector(dragging(gestureRecognizer:)))
sticker.addGestureRecognizer(draggingGesture)
pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinched(gestureRecognizer:)))
sticker.addGestureRecognizer(pinchGesture)
rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotated(gestureRecognizer:)))
sticker.addGestureRecognizer(rotationGesture)
tapGestureAlpha = UITapGestureRecognizer(target: self, action: #selector(setAlpha0(gestureRecognizer:)))
self.managedView.workbenchView.addGestureRecognizer(tapGestureAlpha)
tapGesture = TapGesture(target: self, action: #selector(tapped(gestureRecognizer:)))
tapGesture.idIdentifier = sticker.stickerIdentifier
sticker.addGestureRecognizer(tapGesture)
dTapGesture = DeleteTapGesture(target: self, action: #selector(cancelTapped(gestureRecognizer:)))
dTapGesture.stringIdentifier = sticker.stickerIdentifier
sticker.xButton.addGestureRecognizer(dTapGesture)
sticker.xButton.isUserInteractionEnabled = true
sticker.isUserInteractionEnabled = true
sticker.layer.zPosition = 0
draggingGesture.delegate = self
self.managedView.workbenchView.addSubview(sticker)
你可以试试
stickerArray.forEach {
if [=10=].xButton == gestureRecognizer.view {
[=10=].removeFromSuperview()
}
}
您可能有类似的 stringIdentifier
商品