Xamarin - 使用CoreGraphics.CGAffineTransform进行旋转

时间:2016-08-23 16:14:08

标签: xamarin xamarin.ios async-await core-graphics core-animation

我希望在内容下载时让UI图像旋转,我似乎无法使其旋转。

我有这个事件:

public override void ViewDidAppear(bool animated)
{
    NoConnectionView.Hidden = CheckConnectivityStatus();
    Task.Run(async () => await StartSpinningAnimation(true));
}

然后激发这种方法:

protected async Task StartSpinningAnimation(bool IsSpinning) 
{
    do
    {
        UpdateActiveImage.Transform = CoreGraphics.CGAffineTransform.MakeRotation((float)Math.PI / 4);
    }
    while (IsSpinning);
    return;
}

在下载文件后页面最终会发生变化,所以我只想让它永远旋转。它确实具有动画效果。有没有人有任何想法?

2 个答案:

答案 0 :(得分:1)

使用C#和Xamarin.iOS开发iOS是一个与Native语言略有不同的字母,每当你想在后台线程中调用一些UI元素时(就像你需要让一些UI做动画一样),你必须使用:

InvokeOnMainThread(delegate {
    //Do something related UI stuff
});

如果你为它添加一个try catch,你会得到一个例外,比如“你正在调用一个只能在UI线程中调用的方法”;

顺便说一下,你不能只用一段时间制作动画,我为你写了一个样本,你可以看看:

public partial class ViewController : UIViewController
{
    bool needAnimate = true;
    int count = 0;
    UIView animationView = new UIView();

    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        animationView.Frame = new CoreGraphics.CGRect (50, 50, 100, 100);
        animationView.BackgroundColor = UIColor.Red;
        this.Add (animationView);

        this.View.AddGestureRecognizer (new UITapGestureRecognizer (() => {
            Task.Run(async () => await StartAnimation());
        }));
    }

    private async Task StartAnimation() 
    {
        do
        {
            Console.WriteLine ("count = " + count++);
            InvokeOnMainThread(delegate {
                UIView.Animate(0.25,delegate {
                    animationView.Transform = CoreGraphics.CGAffineTransform.MakeRotation((float)Math.PI / 4 * (count/4 + 1));
                });
            });
            System.Threading.Thread.Sleep(250);
        }
        while (needAnimate);
        return;
    }

    public override void DidReceiveMemoryWarning ()
    {
        base.DidReceiveMemoryWarning ();
        // Release any cached data, images, etc that aren't in use.
    }
}

动画不顺畅,你需要自己优化。

如果你还有一些问题,请离开这里,我会检查后者。

希望它可以帮助你并欢迎来到Xamarin。

答案 1 :(得分:0)

我会使用UIView.AnimateNotify使用CoreGraphics执行动画,并完全避免旋转循环来执行此类操作。

将图像旋转180度并向后重复直到停止的示例:

bool _animateStopping = false;
protected void SpinningAnimation(bool animate)
{
    if (_animateStopping) return;
    _animateStopping = !animate;
    if (animate)
    {   
        UIView.AnimateNotify(1,
        () =>
            {
                image.Transform = CGAffineTransform.MakeRotation((nfloat)(Math.PI)); // in one second, spin 180 degrees
            },
        (bool finished) =>
        {
            UIView.AnimateNotify(1,
            () =>
                {
                    image.Transform = CGAffineTransform.MakeRotation(0); // in one second, spin it back
                },
            (bool finished2) =>
            {
                SpinningAnimation(true);
            });
        });
     }
    else {
        UIView.AnimateNotify(1,
        () =>
            {
                image.Alpha = 0; // fade away in one second
            },
        (bool finished) =>
        {
            image.RemoveFromSuperview();
            _animateStopping = false;
        }
    );}
}

用法:

image = new UIImageView(new CGRect(100, 100, 100, 100));
image.Image = UIImage.FromFile("wag.png");
Add(image);
SpinningAnimation(true);
await Task.Delay(5000); // simulate performing downloads, when done stop the animiation
SpinningAnimation(false);
相关问题