如何更改UIImage或UIImageView的一半的alpha值

时间:2011-03-08 06:46:36

标签: iphone objective-c core-animation uiimage alpha

我正在尝试创建一个动画效果,其中alpha值在开始时为0.5(大约)的图像逐渐变为1,但与传统效果不同。这是我正在寻找的。

原始图片。

enter image description here

一段时间后

enter image description here

经过一段时间后

enter image description here

并在最后

enter image description here

所以基本上如果图像部分的alpha值可以减少,我可以显示填充玻璃或其附近物体的动画。
我不知道这是否可以完成,或者是否可以使用核心图形或核心动画。

P.S。它们是在photoshop中制作的 有没有人对如何实现这种动画有什么想法?

5 个答案:

答案 0 :(得分:4)

您可以在上方添加视图并更改其框架。最好的方法。

如果你正在使用图像,那么sunclass UIView并实现

-(void) drawRect:(CGRect) rect {
//if you want to clip the image, the frame for the view must be smaller then rect 
    [image drawInRect: rect];
}

答案 1 :(得分:4)

我建议如下:

  1. 创建在视图中显示图像的UIImageView。
  2. 创建一个具有背景颜色和透明度的新UIView:

    UIView *transpView = [UIView new];
    tranpsView.alpha = 0.4;
    transpView.frame = self.view.frame;
    [self.view addSubview:transpView];
    
  3. 为透明视图设置动画以向上滑动:

    [UIView animateWithDuration:1.0 animations:^{
        transpView.frame = CGRectOffset(transpView.frame, 0, -tranpsView.frame.size.height);
    }];
    
  4. 完成后不要忘记释放tranpsView。可能是completed块。

    要获得“玻璃填充”类型的动画,只需相应更改frame的{​​{1}}即可。 tranpsView可以是任何UIView子类(如液体或其他东西的纹理)。

    希望有所帮助。!

答案 2 :(得分:3)

这是另一个想法。

使用alpha = 0.5将UIView *view的背景设置为所需的image.png。让

w = view.bounds.size.width;
h = view.bounds.size.height;

使用alpha = 0.5创建另一个UIView *glassView,并将其添加为子视图:

[view addSubview:glassView];
glassView.frame = CGRectMake(0.0, h, w, 0.0); 

创建UIImageView *glassImageView并将其image设置为image.png,并将其添加为glassView的子视图:

[glassView addSubview:glassImageView];
glassImageView.frame = CGRectMake(0.0, -h, w, h); 

现在设置动画以增加glassView的高度和alpha值,同时保持imageGlassView的大小,如下所示:

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
        [UIView setAnimationDelegate:self];
        glassView.alpha = 1.0;
        glassView.frame = CGRectMake(0.0, 0.0, w, h); 
        glassImageView.frame = CGRectMake(0.0, 0.0, w, h);
} [UIView commitAnimations];        

公平地说,我没有对此进行过测试,但是这些方面似乎可能有用。然后,也许不是。如果您找到了完美的解决方案,请将其发回。我很想知道怎么做!

答案 3 :(得分:2)

答案 4 :(得分:1)

嗯,我知道这个问题已经很老了,而且我是那个问过它的人,但是今天我正在尝试类似的东西,并且发现这个简单的解决方案来解决我10个月前提出的问题。

我正在制作一个项目,要求图像看起来像是从完全白色的加载,0%进展到完全红色图像,这是100%的进展。

我想添加2个包含图像的imageViews并将包含图像的顶部imageview的内容模式设置为底部,这样当你缩小imageview的大小时,图像似乎正在加载它工作。对于一些动画块,图像似乎正在加载。然后我想到了这个问题,我问了很久,然后提出了解决我自己问题的方法。

所以这个问题的解决方案是,如果你在彼此的顶部添加2个imageViews,然后减少后面的alpha值,将高度降低到零,然后逐渐增加高度并改变y坐标具有alpha值1的图像视图然后它将在此问题中按照要求加载。

我已经使用这两个函数来获得加载图像的错觉。

- (void)startAnimation
{
    CGRect rect = loadedImageView.frame;
    rect.origin.y = unloadedImageView.frame.origin.y;
    rect.size.height = unloadedImageView.frame.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:2.0f];
    [UIView setAnimationDidStopSelector:@selector(stopAnimation)];
    loadedImageView.frame = rect;
    [UIView commitAnimations];
}

- (void)stopAnimation
{
    CGRect rect = loadedImageView.frame;
    rect.origin.y = unloadedImageView.frame.size.height + unloadedImageView.frame.origin.y;
    rect.size.height = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:2.0f];
    [UIView setAnimationDidStopSelector:@selector(startAnimation)];
    loadedImageView.frame = rect;
    [UIView commitAnimations];
}