重复点击后,CGAffineTransformRotate不会返回原始状态

时间:2017-12-12 14:58:27

标签: ios animation

只需单击一下,ImageView就会旋转,然后返回原始状态。但是,在反复快速单击时,图像视图不会返回到原始状态。任何想法为什么不呢?这是代码:

int repeatCounter = 20;

NSLog(@"Rotate animate started");
[imageView.layer removeAllAnimations];

// Set image
if ([imageView isEqual:rideImageView]){
    [rideImageView setImage:rideImage];
}
else{
    [crashImageView setImage:crashImage];
}

CGAffineTransform rotateStart = CGAffineTransformRotate(imageView.transform, 0.2);
CGAffineTransform rotateEnd = CGAffineTransformRotate(imageView.transform, 0.0f);


// this spin completes 360 degrees every 2 seconds
[UIView animateWithDuration:duration/repeatCounter
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [UIView setAnimationRepeatAutoreverses:YES];
                     [UIView setAnimationRepeatCount:repeatCounter];
                     imageView.transform = rotateStart;
                 }
                 completion:^(BOOL finished) {
                           NSLog(@"Rotate animate completed");

                     imageView.transform = rotateEnd;

                     if (finished) {

                         NSLog(@"Rotate animate finished but after repeated clicks is permanently rotated");

                         if ([imageView isEqual:rideImageView]){
                             [rideImageView setImage:rideLabelledImage];
                         }
                         else{
                             [crashImageView setImage:crashLabelledImage];
                         }
                     }
                    }
                 ];

1 个答案:

答案 0 :(得分:0)

需要使用CGAffineTransformMakeRotation(0);作为轮换结束。这是代码:

 [imageView.layer removeAllAnimations];

// Set image
[crashImageView setImage:crashImage];

CGAffineTransform rotateStart = CGAffineTransformRotate(imageView.transform, 0.2);
CGAffineTransform rotateEnd  = CGAffineTransformMakeRotation(0);

int repeatCounter = 20;

// Rattles to and fro
[UIView animateWithDuration:duration/repeatCounter
                      delay:0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     [UIView setAnimationRepeatAutoreverses:YES];
                     [UIView setAnimationRepeatCount:repeatCounter];
                     imageView.transform = rotateStart;
                 }
                 completion:^(BOOL finished) {
                     //NSLog(@"Rotate animate completed");

                     // Ensure back to original state whether or not animation has completely finished
                     imageView.transform = rotateEnd;

                     if (finished) {

                         //NSLog(@"Rotate animate finished but after repeated clicks is permanently rotated");
                         [crashImageView setImage:crashLabelledImage];
                     }
                 }
 ];