views:

72

answers:

2

Think about a speedometer, and imagine all these little strokes around it like 10 mph, 20 mph, 30 mph, and so on. I try to draw something similar.

Imagine this: There are three views: A, B, C.

A owns B owns C. C is a stroke on that speedometer. So to create it easily, there is view B inbetween which is exactly same size of A. First, C is positioned centered at the top with y=0. That's simple. And then, B is rotated by 10 degrees. Also simple. I keep adding "copies" of B+C on top of A, until I have all strokes on that speedometer.

But now: That's all a big waste of memory, because B is just garbage. It contains only C and is just used once to bring C easily into the desired position. I want to get rid of B, and instead position C directly on top of A.

How could I do this? Could I convert the coordinates from C into those of A, then get rid of B and position C directly on A? Notice that B was rotated and as big as A. It's hard to explain. Let me know if you need further descriptions to follow.

+2  A: 

This question is vague, but here are three ideas that might help a little:

(1) Instead of getting rid of B, can you just get rid of A?

(2) The UIView method convertPoint:toView: -- You might be able to call this on B to find the coordinates of C in terms of A, and use that to move B's subviews to A.

(3) Instead of rotating a view, you could use some good ol' trig to add the small subviews or even draw directly where you want them.

For example, here's something that might help draw tick marks around a speedometer:

CGPoint from, to;
CGFloat innerRadius = 50.0;
CGFloat outerRadius = 60.0;
for (float angle = 0; angle < M_PI*2; angle += M_PI/100) {
  from.x = innerRadius * cos(angle);
  from.y = innerRadius * sin(angle);
  to.x = outerRadius * cos(angle);
  to.y = outerRadius * sin(angle);
  [self drawLineFrom:from to:to];
}

To be useful, you'd probably want to translate those points (which have rotational center (0,0)) to be centered around a point in your view.

Tyler
(1) unfortunately no, I would have to change almost everything in the app. (2 + 3) sounds good!
HelloMoon
+1  A: 

I think you can get rid of B pretty easily by adding C, then setting the anchor point of C to be the center of the circle (I assume y=A.frame.size.height, x is centered), and then you can apply the rotation to C and it should rotate around that anchor getting the desired effect.

Something like

C.layer.anchorPoint = CGPointMake(A.frame.size.height, A.center.x);
C.transform = CGAffineTransformMakeRotation(angle);

Or you could also add C to the center of A, then create a concatenation transform of a translation up and a rotation.

i.e

CGAffineTransform trans1 = CGAffineTransformMakeRotation(angle);
CGAffineTransform trans2 = CGAffineTransformMakeTranslation(0, A.frame.size.height);
C.transform = CGAffineTransformConcat(trans1, trans2);

Though if you are really going to be drawing all those tick marks, Tyler's drawing solution is probably better.

kiyoshi
Both are great, but your's was easier to apply in my case. Thanks both kiyoshi and Tyler. Voted up both.
HelloMoon
This code is pretty simple -- looks good.
Tyler