开发者

Scaling subviews nested deep within UIScrollView

开发者 https://www.devze.com 2023-02-06 13:01 出处:网络
This is causing me a headache of untold proportions. I am trying to create a UML Drawing application for the iPad.

This is causing me a headache of untold proportions.

I am trying to create a UML Drawing application for the iPad.

I currently have functionality to add classes to a 'canvas'. The canvas is simply a UIView, which is located within a UIScrollView.

Each class element is a UIView containing the class name, attributes and methods (UITextFields). These are all subviews of the canvas.

The problem I'm having is when I zoom in on the classes using the pinch/zoom functionality I'm just getting blurry UITextFields. To my understanding, this is because the UIScrollView just applies a transform, and that I need to handle scaling myself.

The only way I can think of doing this however is to redra开发者_运维百科w the classes and make them larger, which could potentially distort their placement relative to each other. For example if I had two classes side by side, and one doubled in size, then the left class may overlap the right one.

I've searched for hours for a solution and I'm getting nowhere, does anyone know of a code sample or advice that can illustrate a situation resembling mine? Or, if not, an example that shows how to scale a UITextField (or UILabel) in the manner I've described?


I'm thinking this may not be the most obvious answer based on how my question was phrased. But I solved the problem like so:

The first step is to implement the:

- (void)scrollViewDidEndZooming:(UIScrollView
*)scrollView withView:(UIView *)view atScale:(float)scale;

The key point I seemed to be missing (somehow) was that when the UIScrollView is scaled, it then resets it's scale factor to 1. So imagine you zoomed to 125%, and then zoomed out 25%, you'd actually be looking at your original view at 93% (ish), not 100%.

This obviously throws your drawing if, like me, you were trying to scale the subviews in relation to their original size.

The best way then is to keep a track of the scale (I used the variable scaleTracker) and then use this value to scale your views.

More specifically for my problem, the following code was used:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{

scaleTracker *= scale;

[canvas setTransform:CGAffineTransformIdentity];

canvas.frame = CGRectMake((int)canvas.original.origin.x,
                              (int)canvas.original.origin.y,
                              (int)(canvas.original.size.width * scaleTracker),
                              (int)(canvas.original.size.height * scaleTracker));
}

Good luck to anyone else who has issues with UIScrollView. For now I hope this can offer some help to any who are interested.

0

精彩评论

暂无评论...
验证码 换一张
取 消