intro: functionality of adding multiple stickers/emoji view.
setup: there 2 view controllers - 1 we're adding stickers, , collectionview of stickers. stickers passed in arrays in 'prepareforsegue
' func. there 2 arrays, 1 sticker images, - uiimageviews
- stickers panned, pinched , rotated.
bug: after adding 2nd sticker, addingstickersvc
reappears previous sticker isn't left if. pinched , zoomed, not panned. new stickers sticked first 1 (same frame?). can pinch , zoom previous stickers separately new ones - (nope, have own frames), can't separate them. end having stack of uiimageviews takes rotating , pinching separately pans together. also, speed of panning increasing after each additional sticker (the panning gesture added multiple time?).
hierarchy of views
stickers added 'viewforemoji' view (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 { emojiview1 in arrayofemojiviews { viewforimgandemoji.addsubview(emojiview1) } } // image out of array. if arrayofemojis.count != 0 { 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 won't add previous emoji. new. if viewforimgandemoji.viewwithtag(n) == nil { viewforimgandemoji.addsubview(emojiview) } n += 1 } } } override func prepare(for segue: uistoryboardsegue, sender: any?) { if arrayofemojis.count != 0 { j in 1...n { if var view1 = self.viewforimgandemoji.viewwithtag(j) as? uiimageview { arrayofemojiviews.append(view1) print("zhenya: views frame \(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 }
and emojicollectionvc:
func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) { let cell = collectionview.cellforitem(at: indexpath) as! emojicollectioncell let chosenemoji = cell.emojiview.image 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 } } } }
found solution. there reason why new imageviews stacked - because after panning imageviews didn't change location in view. whole f view moving. (to find this, spent 8hours tracking changes of frame origins 'prints' @ every step of program). , reason whole view moving - because panning gesture added whole view. instead of
let pan = uipangesturerecognizer(target: self, action: #selector(self.handlepan(recognizer:))) pan.delegate = self viewforimgandemoji.addgesturerecognizer(pan)
i needed:
let pan = uipangesturerecognizer(target: self, action: #selector(self.handlepan(recognizer:))) pan.delegate = self emojiview.addgesturerecognizer(pan)
what interesting, pinching , rotating - still add them whole view contains emojiview:
viewforimgandemoji.addgesturerecognizer(pinch) ... viewforimgandemoji.addgesturerecognizer(rotate)
Comments
Post a Comment