UIImageViews /向视图添加多个贴纸。

时间:2017-04-05 20:39:42

标签: ios swift uiimageview uigesturerecognizer

说明: 向视图添加多个贴纸/表情符号的功能。

设置: 有2个视图控制器 - 一个我们正在添加贴纸,另一个带有贴纸的集合视图。 贴纸本身在' prepareForSegue' FUNC。 有2个阵列,一个只有贴纸图像,另一个 - UIImageViews - 贴纸已经平移,挤压和旋转。

错误: 添加第二个贴纸后,AddingStickersVC重新出现,但之前的贴纸不在我们离开的地方。它被挤压和缩放,但没有平移。还有新的贴纸贴在第一个(同一帧?)上。 我们可以将以前的贴纸与新贴纸分开捏合和缩放 - (不,它们有自己的框架),但我们无法将它们分开。 最终拥有一堆UIImageViews,它们分别进行旋转和捏合,但是一起进行平移。 此外,每增加一个贴纸后平移速度会增加(平移手势会多次添加?)。

观点层次结构

enter image description here

贴纸被添加到' viewForEmoji'查看(viewForImgAndEmoji)。

AddingStickersVC:

@IBOutlet weak var viewForImgAndEmoji: UIView!
@IBOutlet weak var mainImg: UIImageView!
@IBOutlet weak var viewForSnapshot: UIView!


var imageData: Data!
var imageItself: UIImage!
var currentUserPostRef: FIRDatabaseReference!
var emojiImage: UIImage!
var geoFire: GeoFire!

var arrayOfEmojis = [UIImage]()
var arrayOfEmojiViews = [UIImageView]()

var n:Int = 1

override func viewDidLoad() {
    super.viewDidLoad()

    if imageData != nil {
        let img = UIImage(data: imageData)
        let fixedImg = img!.fixOrientation(img: img!)

        mainImg.image = fixedImg
    } else if imageItself != nil {
        mainImg.image = imageItself
    }


    if arrayOfEmojiViews.count != 0 {
        for emojiView1 in arrayOfEmojiViews {
            viewForImgAndEmoji.addSubview(emojiView1)
        }
    }


    // get image out of array.

    if arrayOfEmojis.count != 0 {
        for emoji in arrayOfEmojis {

            let emojiView = UIImageView(image: emoji)
            emojiView.tag = n
            emojiView.frame = CGRect(x: 153, y: 299, width: 70, height: 70)
            emojiView.isUserInteractionEnabled = true

            let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
            pan.delegate = self
            viewForImgAndEmoji.addGestureRecognizer(pan)

            let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(recognizer:)))
            pinch.delegate = self
            viewForImgAndEmoji.addGestureRecognizer(pinch)

            let rotate = UIRotationGestureRecognizer(target: self, action: #selector(self.handleRotate(recognizer:)))
            rotate.delegate = self
            viewForImgAndEmoji.addGestureRecognizer(rotate)

            // check so we won't add previous emoji. only new. 
            if viewForImgAndEmoji.viewWithTag(n) == nil {

                viewForImgAndEmoji.addSubview(emojiView)
            }
            n += 1

         }
    }
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if arrayOfEmojis.count != 0 {
        for j in 1...n {

            if var view1 = self.viewForImgAndEmoji.viewWithTag(j) as? UIImageView {
                arrayOfEmojiViews.append(view1)
                print("Zhenya: views frame is \(view1.frame)")
            }
        }

    }

    if segue.identifier == "EmojiCollectionVC" {
        if let emojiCollection = segue.destination as? EmojiCollectionVC{
            if let image = sender as? UIImage {
            emojiCollection.userImage = image

                if arrayOfEmojis.count != 0 {
                  //arrayToStoreEmojis
                   emojiCollection.arrayToStoreEmojis = arrayOfEmojis
                   emojiCollection.arrayToStoreEmojiViews = arrayOfEmojiViews
            }
        }
      }
    }
}

 @IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.viewForImgAndEmoji)
    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x + translation.x,
        y:view.center.y + translation.y)
        }
    recognizer.setTranslation(CGPoint.zero, in: self.viewForImgAndEmoji)
 }


@IBAction func handlePinch(recognizer: UIPinchGestureRecognizer) {

    let pinchPoint = recognizer.location(in: viewForImgAndEmoji)
    let ourEmojiView = viewForImgAndEmoji.hitTest(pinchPoint, with: nil)

    ourEmojiView!.transform = ourEmojiView!.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
    recognizer.scale = 1

}

@IBAction func handleRotate(recognizer: UIRotationGestureRecognizer){

    let rotatePoint = recognizer.location(in: viewForImgAndEmoji)
    let ourEmojiView = viewForImgAndEmoji.hitTest(rotatePoint, with: nil)

    ourEmojiView!.transform = ourEmojiView!.transform.rotated(by: recognizer.rotation)
    recognizer.rotation = 0
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

和EmojiCollectionVC:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell = collectionView.cellForItem(at: indexPath) as! EmojiCollectionCell
    let chosenEmoji = cell.emojiView.image as UIImage!

    arrayToStoreEmojis.append(chosenEmoji!)
    performSegue(withIdentifier: "backToEmojiVC", sender: arrayToStoreEmojis)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "backToEmojiVC"{

        if let destinationVC = segue.destination as? EmojiVC {
            if let array = sender as? [UIImage] {

                destinationVC.arrayOfEmojis = arrayToStoreEmojis
                destinationVC.arrayOfEmojiViews = arrayToStoreEmojiViews


                let data = UIImagePNGRepresentation(userImage)
                destinationVC.imageData = data
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

找到解决方案。 所有新的imageViews都被堆叠的原因 - 因为在平移后,imageViews没有在视图中更改它们的位置。整个视图都在移动。 (为了找到这个,用8小时跟踪帧起源的变化,并在程序的每一步都打印出来)。 整个视图移动的原因 - 因为平移手势被添加到整个视图中。 而不是

let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pan)

我需要:

let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
emojiView.addGestureRecognizer(pan)

有趣的是,对于捏合和旋转 - 我们仍然将它们添加到包含emojiView的整个视图中:

 viewForImgAndEmoji.addGestureRecognizer(pinch)
 ...

 viewForImgAndEmoji.addGestureRecognizer(rotate)