旋转UIImageView移动其中心

时间:2014-05-20 13:35:05

标签: ios uiimageview interface-builder cgaffinetransform uianimation

我正在尝试使用 CGAffineTranformationMakeRotation()函数定期旋转UIImageView .....

以下是代码....

       - (void)showSpinnerAnimation
       {
               rotateAngle += 3.14f;

              [UIView animateWithDuration:0.18 animations:^{
                      self.spinnerImageView.transform = CGAffineTransformMakeRotation(rotateAngle); 
              } completion:^(BOOL finished) {
                      [self showSpinnerAnimation];
              }];
       }

但我观察的是

Along with the rotating UIImageView. It is also shifting from its center.

我尝试了之前关于此问题的SO问题,因此我尝试了以下方法

1. Set the center of the UIImageView in the completion code (by copying the center in a CGPoint variable).
2. Tried with setting autolayout. But it will break other constraints which i already set for other UI elements in IB.

请注意,self.spinnerImageView是IB的IBOutlet UIImageView。

1 个答案:

答案 0 :(得分:1)

试一试

- (void) rotateView:(UIView *)theView
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue =[NSNumber numberWithFloat: -M_PI];
    rotationAnimation.duration = 0.7;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = HUGE_VALF;

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

停止旋转从图层中删除动画

-(void)stopRotating:(UIView *)theView
{
    [theView.layer removeAnimationForKey:@"rotationAnimation"];
    // or
    //[theView.layer removeAllAnimations];
}

将UIImageView传递给方法。

[self rotateView:self.spinnerImageView]