将图标旋转180度

时间:2017-04-25 07:13:25

标签: ios animation xamarin

我是Xamarin iOS开发的新手,我在点击时尝试将图标旋转180度。
我为旋转编写了以下方法。

private void titleViewTapped(UITapGestureRecognizer tap)
    {
        var rotationAnimation = new CoreAnimation.CABasicAnimation();
        rotationAnimation.KeyPath = "transform.rotation.z";
        rotationAnimation.To = new NSNumber(Math.PI);
        rotationAnimation.Duration = 0.2;
        rotationAnimation.RemovedOnCompletion = false;
        rotationAnimation.FillMode = CoreAnimation.CAFillMode.Forwards;

        triangleIcon.Layer.AddAnimation(rotationAnimation, "rotationAnimation");
    }

当我第一次点击标题时,该程序运作良好 但是,当我再次点击标题时,图标旋转了360度,而不是180度 如何解决这个问题?

我自己解决了这个问题。

private void titleViewTapped(UITapGestureRecognizer tap)
    {
        UIView.Animate(0.2,
            () => {
                triangleIcon.Transform = CGAffineTransform.Rotate(triangleIcon.Transform, (nfloat)Math.PI);},
            () => { }
        );
    }

2 个答案:

答案 0 :(得分:0)

试试这个

  - (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

答案 1 :(得分:0)

我自己解决了这个问题。

private void titleViewTapped(UITapGestureRecognizer tap)
    {
        UIView.Animate(0.2,
            () => {
                triangleIcon.Transform = CGAffineTransform.Rotate(triangleIcon.Transform, (nfloat)Math.PI);},
            () => { }
        );
    }