title: Swift: Cant change CALayer background color in array
body: <p>I am simply trying to change the background color of the last element of a CALayer array. Here is my entire View Class, however its only 2-3 lines that I actually try to access the last element of the CALayer.</p>
  
  <p>Here is my progressViewClass and I put comments to where exactly my problem is:</p>
  
  <pre><code>class ProgressBarView: UIView {
  
  //Variables for progress bar
  var holdGesture = UILongPressGestureRecognizer()
  let animation = CABasicAnimation(keyPath: "bounds.size.width")
  var layerHolder = [CALayer]()
  var widthIndex = CGPoint(x: 0, y: 0)
  var nextXOffset = CGFloat(0.0)
  var checkIfFull = CGFloat()
  var newLayer : CALayer?
  var progressBarAnimationDuration : CFTimeInterval = (MainController.sharedInstance.totalMiliSeconsToRecord  / 10)
  
  // Only override draw() if you perform custom drawing.
  // An empty implementation adversely affects performance during animation.
  override func draw(_ rect: CGRect) {
  
  }
  
  func startProgressBar(){
  
      if(RecordingViewController().currentCameraMode == .recordingMode || RecordingViewController().currentCameraMode == .record1stClipMode) {
  
          newLayer = CALayer()
          newLayer?.frame = CGRect(x: nextXOffset, y: 0, width: 0, height: self.bounds.height)
          newLayer?.backgroundColor = UIColor(red:0.82, green:0.01, blue:0.11, alpha:1.0).cgColor
  
          //print("before \(nextXOffset)")
          newLayer?.anchorPoint = widthIndex
          animation.fromValue = 0
          animation.toValue = self.bounds.width  - nextXOffset
          animation.duration = progressBarAnimationDuration - ((MainController.sharedInstance.miliSecondsPassed) / 10)
          self.layer.addSublayer(newLayer!)
  
          //print("Long Press Began")
          newLayer?.add(animation, forKey: "bounds.size.width")
  
      }
      else{
          stopProgressBar()
      }
  }
  
  func stopProgressBar(){
      if(RecordingViewController().currentCameraMode != .recordingMode){
  
              pauseLayer(layer: newLayer!)
  
              newLayer?.frame = (newLayer?.presentation()!.frame)!
              nextXOffset = (newLayer?.frame.maxX)!
              layerHolder.append(newLayer!)
              print("Layerholder has elements : \(layerHolder.count)")
      }
  }
  
  // HERE IS MY PROBLEM
  func highlightLastLayer(){
      print("in highlight last layer Layerholder has elements : \(layerHolder.count)")
  
      // I CAN HIDE THE CALAYER SO I BELIEVE IM ACCESSING THE CORRECT LAYER
      // layerHolder.last?.isHidden = true
  
      // This is suppose to change the last element background color to blue but doesnt
      layerHolder.last?.backgroundColor = UIColor.blue.cgColor
  }
  
  // ALSO MY PROBLEM
  func unhighlightLastLayer(){
      print("inside unhighlight last layer")
  
      // I CAN HIDE THE CALAYER SO I BELIEVE IM ACCESSING THE CORRECT LAYER
      //layerHolder.last?.isHidden = false
  
      // Changes CALayer back to red
      layerHolder.last?.backgroundColor = UIColor(red:0.82, green:0.01, blue:0.11, alpha:1.0).cgColor
  }
  
  //Function to pause the Progress Bar
  func pauseLayer(layer : CALayer){
      let pausedTime : CFTimeInterval = layer.convertTime(CACurrentMediaTime(), from: nil)
      layer.speed = 0.0
      layer.timeOffset = pausedTime
  
  }
  
  
  
  }
  </code></pre>
  
  <p>Simply put, I create a progressView object in my viewController and then call those functions based on certain button input. This view is essentially a progress bar that you'd see in many video recording applications to show how much you have recorded. In the highlightLastLayer, I am trying to grab the last element of the "layerHolder" array and change its color to blue. Simple right? Doesn't work. Any ideas?</p>