Search code examples
iosuiviewcgcontextclippingcgpath

Clipping parent view by subview and put content of first one in second one but scaled


the question is...

I have a parent view in which i drew a planimetry composed by a lot of CGPaths. Well, in this App, i can insert a subview to parent view in which there are drawn others CGPaths (like lines which represent a chair, for example). Now. I would replicate zoom effect like UITextView has got when someone Tap Down, hold Tap, and an hand-glass comes out with content scaled.

I would put a subview in which the background represents parent content clipped in subview rect and scaled of x2 for example. In this way, subview background seems an hand-glass on parent view.

How can i implement this ?

I thought about taking parent context, passing it to subview, and clipping parent context with rect subview. But nothing happens. I red about CGImageContext with witch taking an image representation about parent CGContext, and pass it to subview, but i dont know how implementing it. And, i dont know if it's right way.

Hope someone can help me (with code sample would be awesome)!

Sorry for my english. I tried to do my best :)


Solution

  • Question solved by myself!

    In subview drawrect: method i put this code:

    - (void)drawRect:(CGRect)rect {
      // here we're just doing some transforms on the view we're magnifying,
      // and rendering that view directly into this view.
      // By 'touchPoint' i can select the exact portion of superview which
      // i want render in subview
    
       CGContextRef context = 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));
       [self.superview.layer renderInContext:context];
    }
    

    Hope this can help someone else :)