【问题标题】:iPhone crop part of image, without resizing using animationiPhone裁剪部分图像,不使用动画调整大小
【发布时间】:2012-09-25 13:49:44
【问题描述】:

好的,在我的应用程序中,我有一张图片,我想慢慢地向用户展示。所以我不想调整图像的大小,我想在背景中有完整的图像,然后慢慢地从上到下显示更多的图像。我正在使用我使用动画完成的动画创建一个垂直进度条,并且工作正常。

进度条本身的 alpha 为 0.5,因此您可以看到它后面的图像。我使用动画并在动画制作时拉伸进度条,这只是一个根据进度条设置颜色的视图。我不介意拉伸它,因为它看起来不错。但是我不希望栏后面的图像拉伸,我希望它只显示与进度条动画一样多的图像。所以说进度条完成了80%,那么我应该看到它后面的图像高度的80%,宽度永远不会改变,并且它后面的图像没有调整大小,只是显示了更多。

我尝试使用与图像上的进度条相同的代码,但它看起来不一致,因为进度条将处于不同的级别并且拉伸的图像看起来很奇怪。

我一直在研究动态裁剪代码的不同方法,但我似乎找不到任何在动画中这样做的方法。是否有执行此类任务的内置方法?我发现的很多裁剪代码似乎都在调整图像大小。

我一直在用它来动画我的进度条

 [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    transform = CGAffineTransformMakeScale(1,transformValue);
    image.transform = transform;
    image.center = CGPointMake(xValue, yValue);

[UIView commitAnimations];

我希望你可以使用类似的东西来制作图像的动画。

任何帮助将不胜感激!

经过大量的测试,我设法达到了这一点:

    [UIView animateWithDuration:3.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{


                         UIImage *image = [UIImage imageNamed:@"button_connectMe.png"];
                     CGImageRef tmpImgRef = image.CGImage;
                     CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 30, image.size.width, image.size.height));

                     UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
                    imgView.image = topImage;
                     imgView.frame = CGRectMake(0, 180, topImage.size.width, topImage.size.height);

                 } 
                 completion:^(BOOL finished) {
                     NSLog(@"Final method");


                 }
 ];

这会将图像裁剪成两半,并保持原始图像大小,但问题是,新裁剪的图像会立即应用在这行代码中

 UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
                        imgView.image = topImage;

这不是动画,而这部分

imgView.frame = CGRectMake(0, 180, topImage.size.width, topImage.size.height);

是动画的。我需要以某种方式对 CGI 图像的帧变化进行动画处理,以便将图像慢慢裁剪成所需的任何大小。

【问题讨论】:

    标签: objective-c animation


    【解决方案1】:

    终于!!

    折腾了两天,终于找到答案了。当我发现这个帖子时正在做更多的测试和搜索

    Animating a UIImage or UIImageView?

    我修改了代码以满足我的需要,最终得到了这个

    -(void)cropAnimateBottom
    {
        UIImage *image = [UIImage imageNamed:@"bar.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, image.size.width, 0)];
        imageView.image = image;
        [imageView setClipsToBounds:YES];
        [imageView setContentMode:UIViewContentModeBottom];
    
        [self.view addSubview:imageView];
    
    
        [UIView animateWithDuration:3.0f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^(void) {
                             imageView.frame = CGRectMake(150, 150, image.size.width, -image.size.height);
                         }
                         completion:NULL];
    
    }
    

    我将 contentMode 设置为底部,如您所见,将动画块中的图像高度设置为减去图像高度。如果您想从上到下制作动画,那么只需反过来将内容模式更改为顶部,即

    -(void)cropAnimateTop
    {
        UIImage *image = [UIImage imageNamed:@"bar.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, image.size.width, 0)];
        imageView.image = image;
        [imageView setClipsToBounds:YES];
        [imageView setContentMode:UIViewContentModeTop];
    
        [self.view addSubview:imageView];
    
    
        [UIView animateWithDuration:3.0f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^(void) {
                             imageView.frame = CGRectMake(0, 100, image.size.width, image.size.height);
                         }
                         completion:NULL];
    
    }
    

    希望能帮助其他正在努力实现相同目标的人!

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      相关资源
      最近更新 更多