title: Swift: CALayer bounds contains NaN: [nan nan; nan nan]?
body: <p>I have no idea what is going on here despite looking at similar answers. I have made a custom UIImageview that is supposed to start animating as soon as it is created:</p>
  
  <pre><code>class HeaderAnimator: UIImageView {
  
      var travelUp = Bool(false)
  
      var img = UIImage()
  
      override init(frame: CGRect) {
          super.init(frame: frame)
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
      }
  
      override func layoutSubviews() {
          //init
  
          //ALWAYS SAME COLOR - black with less opacity, just change shapes randomly via parent
          self.image = img
  
          UIView.animate(withDuration: 1,
                         delay: 0.2,
                         options: UIViewAnimationOptions.curveEaseIn,
                         animations: { () -&gt; Void in
                          //animate
                          if(self.travelUp)
                          {
                               self.center.y += screenSize.height * (50/screenSize.height)
                          }
                          else {
                              self.center.y -= screenSize.height * (50/screenSize.height)
                          }
  
          }, completion: { (finished) -&gt; Void in
              // ....
              print("done")
              self.removeFromSuperview()
          })
  </code></pre>
  
  <p>Then Im trying to spawn these views on a timer in another view:</p>
  
  <pre><code>let rand = Double(arc4random_uniform(4))
         var bgTimer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.animateInBg), userInfo: nil, repeats: true)
  
  func animateInBg()
      {
          var randPos = CGPoint()
          var upOrDown = Int(arc4random_uniform(UInt32(2)))
          //print(upOrDown)
  
          let h = HeaderAnimator(frame: CGRect(x: 0, y: 0, width: self.bounds.width * (40/self.bounds.width), height: self.bounds.width * (40/self.bounds.width)))
          h.img = UIImage(named: "rocket")!
  
          switch upOrDown {
          case 0: //down
              randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0.9)
              h.travelUp = false
              //h.transform = CGAffineTransform(scaleX: 1, y: -1)
  
          case 1: //up
              randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0)
              h.travelUp = true
          default:
              randPos = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: ( self.bounds.width)), y: self.bounds.height * 0)
              h.travelUp = true
          }
  
          h.center = CGPoint(x: self.bounds.width * 0.5, y: 0)
          self.addSubview(h)
      }
  </code></pre>
  
  <p>These views are supposed to show up then move either up or down out of the view then delete. Shortly after one spawns however, I get error: <code>CALayerInvalidGeometry CALayer bounds contains NaN: [nan nan; nan nan]</code></p>
  
  <p>What does this mean? What am I doing wrong just creating these views?</p>