我有以下代码行,如果我停在X上删除图像,则会将其删除。问题是,如果我有一个或多个相同类型的图像,将其中一个删除,也会删除所有其他相同的图像。我认为我的问题在于调用“ forEach”。我该怎么用?
如果图像不同,我会正确删除单个“贴纸”。
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)
答案 0 :(得分:0)
您可以尝试
stickerArray.forEach {
if $0.xButton == gestureRecognizer.view {
$0.removeFromSuperview()
}
}
您可能对项目有类似的stringIdentifier