2017-04-25 152 views
-1

我是Xamarin iOS开发人员中的新成员,我尝试在点击时将图标旋转180度。
我为旋转编写以下方法。将图标旋转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);}, 
      () => { } 
     ); 
    } 

回答

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"]; 
} 
+0

谢谢你的评论。 'repeatCount'不应该被使用,因为我不想重复旋转,只需点击 –