我有一个imageview和贴纸,现在我想在贴纸上添加手势,一切正常,但,我的贴纸是从uiimageview的图层出来,如图所示。我只想在ImageView的范围内显示标签的姿态。
swift - 带有贴纸的UIImageView并在贴纸上添加手势-LMLPHP
以下是我的代码:

func addStickers(image: UIImage) {

    gesture = "Stickers"

    imgSticker  = UIImageView(frame: CGRect.init(x: 0.0, y: 0.0, width: 80, height: 80))
    imgSticker.center = view.center
    imgSticker.image = image
    imgSticker.contentMode = UIViewContentMode.scaleAspectFill
    imgSticker.isUserInteractionEnabled = true

    self.view.addSubview(imgSticker)
    //imgImage.addSubview(imgSticker)

    panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(handlePanGesture(_:)))
    panGesture.delegate = self

    pinchGesture = UIPinchGestureRecognizer.init(target: self, action: #selector(handlePinchGesture(_:)))
    pinchGesture.delegate = self

    rotateGesture = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotateGesture(_:)))
    rotateGesture.delegate = self

    imgSticker.addGestureRecognizer(panGesture)
    imgSticker.addGestureRecognizer(pinchGesture)
    imgSticker.addGestureRecognizer(rotateGesture)
}

我调用此函数和句柄方法如下:
                addStickers(image: #imageLiteral(resourceName: "Sticker1.png"))

func handlepangesture(识别器:uipangesturecognizer){
    if gesture == "Stickers" {
        let recognizerCenter = recognizer.location(in: self .view)
        imgSticker.center = recognizerCenter

    } else {
        let recognizerCenter = recognizer.location(in: self.view)
        txtLabel.center = recognizerCenter
    }

}

func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) {
    if gesture == "Stickers" {
         imgSticker.transform = imgSticker.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
         recognizer.scale = 1.0

    } else {
        txtLabel.transform = txtLabel.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
        recognizer.scale = 1.0
    }

}

func handleRotateGesture(_ recognizer: UIRotationGestureRecognizer) {

    if gesture == "Stickers" {
         imgSticker.transform = imgSticker.transform.rotated(by: recognizer.rotation)
         recognizer.rotation = 0.0

    } else {
        txtLabel.transform = txtLabel.transform.rotated(by: recognizer.rotation)
        recognizer.rotation = 0.0
    }

}

最佳答案

尝试将self.view.addSubview(imgSticker)中的self.view替换为正在编辑的图像的imageview,即在其上粘贴标签的图像视图。希望有帮助。
所以,应该是这样-imageView.addSubview(stickerOnImg)。我以前也遇到过同样的问题,这对我很有效。

关于swift - 带有贴纸的UIImageView并在贴纸上添加手势,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46149840/

10-10 18:44