Cross Platform
Android
iOS
 

Animate Using Blocks

This recipe shows how to animate using blocks from Xamarin.iOS.

Recipe



Follow these steps to create the animation:

  • Add an image named sample.png to the project with a Build Action of Content.
  • In a UIViewController subclass create class variables for a UIImageView, UIImage and point.
UIImageView _imageView;
UIImage _image;
PointF _pt;
  • Create the UIImageView, assign it a UIImage and at it as a subview.
_imageView = new UIImageView (new RectangleF (0, 0, 50, 50));
_image = UIImage.FromFile ("sample.png");
_imageView.Image = _image;
View.AddSubview (_imageView);
  • Set the point to the Center property of the UIImageView.
_pt = _imageView.Center;
  • Call UIView.Animate and pass the duration, delay, animation options, and lambda expressions for the animation code and animation completion code.
UIView.Animate (2, 0, UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.Autoreverse,
    () => {
        _imageView.Center = 
            new PointF (UIScreen.MainScreen.Bounds.Right -_imageView.Frame.Width / 2,
                _imageView.Center.Y);},
    () => {
        _imageView.Center = _pt; }
);

Additional Information

Objective-C blocks are bound to NSAction delegates in C#. Therefore, lambda expressions can be used to define the code that will be called for the animation as well as the animation completion callback.