iosuiviewdrawrectsetneedsdisplaycgcontextref

How to correctly update a uiview magnified and translated with CGContextScaleCTM and CGContextTranslateCTM


I have a uiview B that is meant to be a magnified portion of another uiview A (the area around "touchPoint" is magnified). When "touchPoint" changes I want B to update its display. I have the following function in B and it does update (when I call B.setNeedsDisplay()), but it keeps the old content somehow visible, like a faded background image. Do you guys know why the old content still shows up, and why that faded effect? Is it because the area I'm magnifying has a transparent background? How to fix it?

   override func drawRect(rect: CGRect)
   {
      let context: CGContextRef = UIGraphicsGetCurrentContext()
      CGContextTranslateCTM(context,1*(self.frame.size.width*0.5),1*(self.frame.size.height*0.5))
      CGContextScaleCTM(context, 1.5, 1.5)
      CGContextTranslateCTM(context,-1*(touchPoint.x),-1*(touchPoint.y))
      viewToMagnify.layer.renderInContext(context)
   }

I would have liked to post an image, but I don't have enough stackOverflow points yet... :-)

Thanks for any pointers!


Solution

  • Found the answer to my own question.

    The context of the magnified view had to be cleared at the beginning of the drawRect function, with the following:

    CGContextClearRect(context, rect)