LampShade
LampShade

Reputation: 2775

Get angle of rotation after rotating a view

Let's say I rotate a view using the following method:

            CGAffineTransform t = CGAffineTransform.MakeIdentity();
            t.Rotate (angle);
            CGAffineTransform transforms = t;
            view.Transform = transforms;

How can I get the current rotation angle of this view without keeping track of what i put in the angle variable when I initially did the CGAffineTransform? Is it related to the view.transform.xx/view.transform.xy values?

Upvotes: 0

Views: 332

Answers (1)

Grx70
Grx70

Reputation: 10349

Not sure what these xx and xy and all other similar members mean exactly, but my guess* is that You won't be able to trace back the applied transformations using solely those values (it would be like tracing back 1+2+3+4 only knowing that You started off with 1 and ended up with 10 - I think*).

In that case my suggestion would be to derive from the CGAffineTransform and store the desired values, but since it's a structure You cannot do that, so in my opinion Your best choice is to write a wrapper class, like so:

class MyTransform
{
    //wrapped transform structure
    private CGAffineTransform transform;

    //stored info about rotation
    public float Rotation { get; private set; }

    public MyTransform()
    {
        transform = CGAffineTransform.MakeIdentity();
        Rotation = 0;
    }

    public void Rotate(float angle)
    {
        //rotate the actual transform
        transform.Rotate(angle);
        //store the info about rotation
        Rotation += angle;
    }

    //lets You expose the wrapped transform more conveniently
    public static implicit operator CGAffineTransform(MyTransform mt)
    {
        return mt.transform;
    }
}

Now the defined operator lets You use this class like this:

//do Your stuff
MyTransform t = new MyTransform();
t.Rotate(angle);
view.Transform = t;
//get the rotation
float r = t.Rotation;

//unfortunately You won't be able to do this:
float r2 = view.Transform.Rotation;

As You can see this approach has it's limitations, but You can always use only one instance of MyTransform to apply all sorts of transformations and store that instance somewhere (or, possibly, a collection of such transforms).

You may also want to store/expose other transformations like scale or translate in the MyTransform class, but I believe You'll know where to go from here.



*feel free to correct me if I'm wrong

Upvotes: 1

Related Questions