我有这些代码行,如果我停在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)

最佳答案

你可以试试

stickerArray.forEach {
    if $0.xButton == gestureRecognizer.view {
        $0.removeFromSuperview()

    }
}

您可能有类似的stringIdentifier项目

关于ios - id标识符 swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53579698/

10-12 04:02