Search code examples
xamarin.ioscore-graphicstransformrotationcgaffinetransform

Trying to Use Transform to Rotate a UIView in MonoTouch


I have a UIControl defined in which I have used the MonoTouch.CoreGraphics classes to draw some items in and have put the UIControl into a UIView through AddSubview. I'm trying to take the view and turn the whole thing to simulate something sort of like movement of the minute or second hand on a clock or a dial. I'm under the impression that I can do that with the Transform on the UIView.

The name of my UIView is container. I've tried:

    container.Transform.Rotate(-0.78f);

I have also tried:

    CGAffineTransform t = CGAffineTransform.MakeIdentity();
    t.Rotate(-0.78f);
    container.Transform = t;

I have also tried:

    CGAffineTransform t = CGAffineTransform.MakeRotation(-0.78f);
    container.Transform = t;

I have also tried this and other combinations of it:

    UIView.BeginAnimations("rotate");
    UIView.SetAnimationDuration(0.5);
    container.Transform.Rotate((float)Math.PI);
    UIView.CommitAnimations();

None of the above have had any impact on my display. It does not rotate or move in the slightest. All of the iOS related posts refer to CGAffineTransformRotate, but I can't find a Mono exact match for that and am assuming that is the equivalent of what I am doing above. Is there some other way I should be trying to make my view rotate?


Solution

  • You are on the right track. I have a large game under development and do this all over the place in my code. Here is the simplest example I can give you:

    using MonoTouch.CoreGraphics;
    
    float degrees = 15;
    UIView aView = new UIView(new RectangleF(10,10,10,10));
    aView.Transform = CGAffineTransform.MakeRotation(3.14159f * degrees / 180f);
    

    That's it.

    The examples are sparse, especially the fact that the MakeRotation function is in radians (hence the use of pi to convert to degrees).