The image view, which is a preview, is drawn on top of the holder view. Image image is opaque. This means that when viewing the views, there is no part of the owner’s view that is actually visible, so the drawRect call is optimized.
Try to arrange the views in reverse order, so that the holder view is a sub-image. Then the image will be drawn, and the view of the holder will be drawn on top of it.
Also note that you must use the borders of the parent view as a preview frame.
UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];
Edit (add):
See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1 , in particular in the section View Hierarchy and View Management:
“Visually, the contents of a subclause hide all or part of the contents of its parent view”
So, try to make the figurative representation a parent, do your initialization as follows:
// instance variables: UIImageView* imageView; MyHolderView* holderView; imageView = [[UIImageView alloc] initWithFrame:mainRect]; holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]]; holderView.opaque = NO; holderView.backgroundColor = [UIColor clearColor]; [imageView addSubview:holderView]; UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:@selector(scale:)]; [pinchRecognizer setDelegate:self]; [holderView addGestureRecognizer:pinchRecognizer]; [pinchRecognizer release]; // etc...
Now the image will be drawn, and the view of the holder, its preview will be drawn on top of it. Now, when you call setNeedsDisplay in holderview, it will get drawRect: call.
For example, track a gesture this way. It can be in your view controller or in a subclass of View MyHolderView; an example here will be in the MyHolderView class, so the instance variables location1 and location2 can be easily separated using the drawRect: method:
-(void)scale:(id)sender { if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required // get position of touches, for example: NSUInteger num_touches = [pinchRecognizer numberOfTouches]; // save locations to some instance variables, like `CGPoint location1, location2;` if (num_touches >= 1) { location1 = [pinchRecognizer locationOfTouch:0 inView:holderView]; } if (num_touches >= 2) { location2 = [pinchRecognizer locationOfTouch:1 inView:holderView]; } // tell the view to redraw. [holderView setNeedsDisplay]; } }
and then in manual drawRect mode:
-(void)drawRect:(CGRect)rect {