fzwo
fzwo

Reputation: 9902

Maintain constant line width in zooming UIScrollView subView

I have a zooming UIScrollView that contains a UIView which contains some custom UIViews, of the class MyRectangleView. Both the ScrollView and these rectangleViews get a pointer to a common custom UIViewController, MyViewController.

Is it possible to render these custom views so they appear to grow when the scrollView is zoomed, instead of appearing scaled?

I do override drawRect:, and I have managed to make them change their height, but maintain their width when the scrollView is zoomed, by implementing scrollViewDidZoom: in myViewController and storing the current zoomScale in a property accessible from the rectangleViews, then dividing their widths by the zoomScale.

I've done the same with the line widths, but as a result of all the scaling, the result looks very fuzzy (although line width stays more or less constant, visually).

Is there a better way to do this?

How to implement a custom UIView as a subView of a zooming UIScrollView that provides constant line width and doesn't get fuzzy?

One solution I have thought of would be to make the zoomed views invisible and create other views on top of the scrollView that get their sizes from them, but I'd rather avoid that, if possible.

Upvotes: 1

Views: 2248

Answers (2)

washfaq
washfaq

Reputation: 336

I have successfully used another approach. I wrote a custom method in the subclassed UIView

-(void)resize:(CGFloat)scale {    
self.transform = CGAffineTransformMakeScale(1/scale, 1/scale);

}

Where scale is the current zoomScale of UIScrollView. This method is called from the following in the parent view

-(void)scrollViewDidZoom:(UIScrollView *)_scrollView {

for (PushPin *pushpin in drawingPlaceholder.subviews) {
    if ([pushpin isKindOfClass:[PushPin class]]) {
        [pushpin resize:_scrollView.zoomScale];
    }
}

}

Upvotes: 4

fzwo
fzwo

Reputation: 9902

The answer was to set these rectangleViews as children of the UIScrollView and NOT have them returned by viewForZoomingInScrollView:. This means they will be scrolled, but not zoomed.

In their drawRect:, I transform their frame with the scrollView's zoomScale (provided by the VC). This causes slight artifacts because changing the frame triggers a redraw (without triggering drawRect:), but I'm willing to live with that for the moment.

This means I can set their size as if they were zoomed, but draw them with 1:1 pixels, add text, images, etc., and they remain crisp.

Upvotes: 1

Related Questions