In this recipe, we will rotate a UILabel
by applying a transformation. Furthermore, the rotation will be animated.
Create a new Single View Application in Xamarin Studio and name it TransformViewApp
. Add a label and a button on the controller.
Perform the following steps:
Add the
MonoTouch.CoreGraphics
namespace in theTransformViewAppViewController.cs
file as follows:using MonoTouch.CoreGraphics;
Enter the following
ViewDidLoad
method in theTransformViewAppViewController
class:private double rotationAngle; public override void ViewDidLoad () { base.ViewDidLoad (); this.btnRotate.TouchUpInside += async (sender, e) => { this.rotationAngle += 90; CGAffineTransform rotation = CGAffineTransform.MakeRotation((float)this.DegreesToRadians(this.rotationAngle)); await UIView.AnimateAsync(0.5d, () => this.lblOutput.Transform = rotation); this.lblOutput.Text = string.Format("Rotated to {0} degrees.", this.rotationAngle); ...