UIImage视图从一个图像动画到另一个图像

4
我希望能够流畅地将一个UIImageView中的图像动画转换为下一张图像,就像UIModalTransitionStyleCrossDissolve一样,在一秒钟内完成。
目前,我有一个名为“imageView”的UIImageView *和一个名为“imageForState”的函数,该函数返回正确图像的UIImage *。
因此,如果存在图像B,我想要将图像A平滑地动画转换为图像B。
目前,我的函数如下:
- (UIImage *) imageForState{
    NSLog(@"state: %d", [save state]);
    switch ([save state]) {         
        case 0:
        case 1:
            return [UIImage imageNamed:@"Sakuya_Debug.PNG"];
        case 2:
            return [UIImage imageNamed:@"Sakuya_2_Debug.PNG"];
        default:
            return NULL;
            //Never called
    }
}

-(void) tap{
    [imageView setAnimationImages:[NSArray arrayWithObjects:[imageView image], [self imageForState], nil]];
    imageView.animationDuration = 2.0;
    [imageView startAnimating];
    save.state++;
}

我的问题是,第一,动画会一直持续下去(当我将imageView.animationRepeatCount设置为1时,会再次回到第一张图片),第二,动画不流畅;就像我使用 "setImage" 一样。

我做错了什么?

1个回答

3

我建议使用两个imageView和一个动画块来完成。在下面的示例中,我假设有两个100像素大小的正方形图像。如果将这两个imageViews添加为子视图,请确保在添加imageA后再添加imageB,以便它可以覆盖imageA。

初始化imageViews:

UIImageView *imageA = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Sakuya_Debug.PNG"]];
UIImageView *imageB = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Sakuya_2_Debug.PNG"]];

// hide the second image
imageB.alpha = 0.0;

// put the image with the same size at same position
imageA.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);        
imageB.frame = imageA.frame;

混合方法:

- (void)crossDissolveImageAtoB
{
    [UIView animateWithDuration:1.0 
                     animations:^{
                         imageB.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         // do something after the animation finished, 
                         // maybe releasing imageA if it's not used anymore...
                     }];
}

非常感谢。这真的推动了我前进。 - MechMK1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接