ios - How do you rescue a view that attempts to break a boundary? -


i have container view serve boundary labels in project. have feature allows users change size of view or not can problematic labels adhere boundary since forced out. know type of behavior how uicollisionbehaviors designed prevent or recover "lost" labels.

i have tried turning boundaries on , off before , after changing size of container view doesn't seem help. ideally if label gets popped out recover position , center within container view. when attempt move label center goes before outside container.

any appreciated.

as can see in following example, label has rescue attempt change it's center position not work.

example animated gif

github project

customlabel.swift

import uikit  class customlabel: uilabel {      var containerview: uiview?     var animator: uidynamicanimator?     var boundry: uicollisionbehavior?      func containwithin(view: uiview) {         containerview = view         animator = uidynamicanimator(referenceview: view)         isuserinteractionenabled = true         backgroundcolor = .white     }      func isboundryenabled(_ decider: bool) {         boundry?.translatesreferenceboundsintoboundary = decider     }      override func touchesmoved(_ touches: set<uitouch>, event: uievent?) {         if let animator = self.animator {             animator.removeallbehaviors()              touch in touches {                 let location = touch.location(in: containerview)                 let snap = uisnapbehavior(item: self, snapto: location)                  snap.damping = 0.2                 animator.addbehavior(snap)                  boundry = uicollisionbehavior(items: [self])                 boundry?.translatesreferenceboundsintoboundary = true                  if let b = self.boundry {                     animator.addbehavior(b)                 }             }         }     } } 

viewcontroller.swift

import uikit  class viewcontroller: uiviewcontroller {      var containerview: uiview?     var labels: [customlabel] = []      override func viewdidload() {         super.viewdidload()          let rect = cgrect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.width)         containerview = uiview(frame: rect)         containerview?.center = view.center         containerview?.backgroundcolor = uicolor.blue          if let cv = containerview {             let label = customlabel(frame: cgrect(x: 0, y: 0, width: 0, height: 0))             labels.append(label)             label.text = "hello world"             label.sizetofit()             label.containwithin(view: cv)             view.addsubview(cv)             cv.addsubview(label)         }     }      @ibaction func animatecontainerportrait() {         label in labels {             label.isboundryenabled(false)         }          uiview.animate(withduration: 0.3, animations: {             let rect = cgrect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.width + 150)             self.containerview?.frame = rect             self.containerview?.center = self.view.center         }, completion: { completed in             if completed {                 label in self.labels {                     label.isboundryenabled(true)                 }             }         })     }      @ibaction func animatecontainersquare() {         guard let container = self.containerview else {             print("unable find container view")             return         }          label in labels {             label.isboundryenabled(false)         }          uiview.animate(withduration: 0.3, animations: {             let rect = cgrect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.width)             container.frame = rect             container.center = self.view.center         }, completion: { completed in             if completed {                 label in self.labels {                     if label.frame.maxy > container.frame.height {                          print("attempting rescue label")                         label.center = container.center                     }                      label.isboundryenabled(true)                 }             }         })     } } 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -