UIImage 视图从一张图像到下一张图像的动画

发布于 2024-11-08 14:09:11 字数 961 浏览 0 评论 0原文

我想像 UIModalTransitionStyleCrossDissolve 那样平滑地对 UIImageView 中的图像进行动画处理,在一秒钟内变成下一张图像。

目前,我有一个名为“imageView”的 UIImageView * 和一个名为“imageForState”的函数,它返回正确图像的 UIImage * 。

所以我想做的是将图像 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++;
}

我的问题是,1.)动画永远持续下去(当我将 imageView.animationRepeatCount 设置为 1 时,我再次回到第一张图像)和 b.)动画不平滑;就像我会使用“setImage”一样;

我做错了什么?

I want to smothly animate an image in an UIImageView to turn into the next one within one second like UIModalTransitionStyleCrossDissolve does.

Currently, I've got an UIImageView * called "imageView" and a function which is called "imageForState" which returns me a UIImage * for the correct image.

So what I want to do is to animate the image A smoothly into image B if there is one.

Currently, my function is like that:

- (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++;
}

My problem is, that 1.) the animation goes on forever (when I set imageView.animationRepeatCount to 1, I end up at the first image again) and b.) the animation is not smooth; just like I would have used "setImage";

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-11-15 14:09:11

我建议使用两个 imageView 和一个动画块来实现。在下面的示例中,我假设有两个大小为 100 像素的方形图像。如果将两个 imageView 添加为子视图,请确保将 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;

Blend 方法:

- (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...
                     }];
}

I suggest doing it with two imageViews and an animation block. In the following example I assume two square images with a siza of 100px. If you add the two imageViews as subviews make sure that imageB is added after imageA so it covers it.

Init the 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;

Blend method:

- (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...
                     }];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文