DrawRect UIView inside UIScrollView

I am trying to draw a small image in UIScrollView for my iPhone application. I started with a UIImage created from png, which I included in my package, and this works fine. Each time the zoom / pan stops, the scrollview delegate recounts the frame of this UIImage (which is a subspecies of the UIScrollView contentView) and calls setNeedsDisplay. The frame is getting smaller and smaller in width and height as you zoom in on the scroll image because I want the image to stay on the screen the same size. While the user is scaling the image, this is the wrong size, but as soon as the scaling stops, it is fixed. Not very, but not the worst.

Now the problem is that I want to make the image on my own and not static png. I subclassed the UIView and ended up in the drawRect method, but I cannot get the math to correctly draw a smooth path when zooming in. I thought I could just scale the line width down and the radius of the circle I'm drawing (CGContextAddArc), but it looks like the iPhone tried to draw a thin aligned circle by two or three pixels, and then enlarged those pixels, instead of enlarging the image and draw a very accurate circle above it.

This is a ViewController resizing a UIView (which is a subview of the contents of a UIScrollView)

float scaler = (1/scrollView.zoomScale); [targetView setFrame:CGRectMake(viewCoords.x-11*scaler, viewCoords.y-11*scaler, 22*scaler,22*scaler)]; [targetView setNeedsDisplay]; 

This is drawRect UIView

 float x = rect.origin.x + rect.size.width/2.0; float y = rect.origin.y + rect.size.height/2.0; float radius = (rect.size.width-2.0*lineWidth)/2.0; CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(ctx,lineWidth ); CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); CGContextAddArc(ctx, x, y, radius , 0, 2* M_PI, 1); CGContextClosePath(ctx); CGContextStrokePath(ctx); 

I open the idea of ​​subclassing the UIView in another place, but I have to synchronize it with the UIScrollView contentview

+4
source share
1 answer

See my answer to this question . UIScrollView applies a transform to your view of the content when it does scaling, which causes your blur. You can override this to sharply draw your content if you follow the method that I describe.

+1
source

All Articles