title: Animation and Scrolling Performance Seriously Lagging Using CAShapeLayers for Masking
body: <p>I'm asking this question as a last resort because any solution I've found and tried has yet to produce a positive result. I am designing an app where a user can manipulate a <code>UIView</code> and make a custom shape that is defined by a <code>UIBezierPath</code>.  Without getting into too many specifics, and to avoid boring you this is basically what's going on:</p>
  
  <pre><code>override func drawRect(rect: CGRect) {
  
      // Create the path
      var path = UIBezierPath()
  
      // ...... The path is defined, set to be drawn in the view's frame
  
      // Draw the stroke of the actual path, close it and clip.
      path.lineWidth = 2.0
      UIColor.whiteColor().setStroke()
      path.stroke()
      path.closePath()
      path.addClip()
  
      // Create a CAShapeLayer to crop everything outside the path
      let maskForPath = CAShapeLayer()
      maskForPath.path = path.CGPath
      self.layer.mask = maskForPath
  }
  </code></pre>
  
  <p>So that works great, and as the user continues to use the app, they create more and more shapes and their previously created shapes get stored in a <code>UIScrollView</code> for them to see, select and edit later on.  Anyways once multiple shapes have been created (say 15 or more), any animations that I perform using <code>UIView.animateWithDuration</code> get EXTREMELY choppy, and the same goes for the scrolling inside the <code>UIScrollView</code>.  It's frustrating because everything works great and is fluid up until a large amount of views are created.  Also, I've tried manually moving the views using <code>touchesBegan</code> and <code>touchesMoved</code> and when I move around the views that way they move just fine, there is no lag or anything. </p>
  
  <p>I've exhausted every solution I could find thus far.  Here are some of the things I've tried to this point:</p>
  
  <p>1) 
    a) <code>layer.shouldRasterize = true</code></p>
  
  <p>b) <code>layer.rasterizationScale = UIScreen.mainScreen().scale</code></p>
  
  <p>2) <code>layer.drawsAsynchronously = true</code></p>
  
  <p>3) <code>AsyncDisplayKit</code></p>
  
  <p>4) <code>layer.allowsEdgeAntialiasing = true</code></p>
  
  <p>None of these solutions has yielded a positive result.  Can anyone think of anything I may be missing, or have an idea that could solve this issue?  It doesn't seem to be a memory problem, as even when I have a lot of these views nested inside a <code>UIScrollView</code>, my app is only using an average of 30mb of memory.  Also, the impact of the CPU seems to be minimal.  I'm not using any shadows, and I've tried testing this on an iPhone 5s, 6, and 6s Plus all with the same result, so it doesn't appear to be an issue with the device.  Hopefully someone can help me find a solution because it makes the app look like garbage and the system isn't even being taxed as far as I can tell.  </p>
  
  <p>I appreciate any help! Thanks in advance!</p>