views:

65

answers:

1

Hi everyone

Me again. I have a simple question. I have an UIImageView like the one shown below. alt text

That UIimageView is supposed to be the knob to control the volume of my iphone project. My question is, how to know the positions of bar on the UIImageView when it is rotated? Because the volume needs to be 0.5 when the little bar on the cercle is vertical.

I got a piece of code which is (in the touchMoved method):

float dx = locationT.x  - imgVVolume.center.x;
float dy = locationT.y  - imgVVolume.center.y;


CGFloat angleDif = 0.0f;

movedRotationAngle = atan2(dy,dx);

if (beganRotationAngle == 0.0) {
    beganRotationAngle = movedRotationAngle;
    initialTransform = imgVVolume.transform;
}
else {
    angleDif = beganRotationAngle - movedRotationAngle;
    CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
    imgVVolume.transform = newTrans;
}

Help please.

+1  A: 

It depends on what input mechanism you want to use to control the rotation.

If the knob is to rotate based on a single finger touch dragging from side to side then you can create a UIPanGestureRecognizer and attach it to the knob UIImageView. The translationInView: method returns a CGPoint which is the amount of X and Y movement from the touch-down point. You can feed that into a formula like the one you post to get an angle of rotation. You'll want to keep track of delta from last position and also check for stop limits (like 0..360) to prevent over-rotation.

OTOH, if you're going to use two finger rotation then you'll want to use a UIRotationGestureRecognizer and look for the rotation value. Just feed that into a CGAffineTransformRotate and set it to the UIImageView transform. That takes care of all of the above for you. Again, you'll want to check for stop limits.

Ramin
could you please provide an example? It's a single finger touch I confirm :D
okayasu