简体   繁体   English

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

[英]how change the alpha value of half of UIImage or UIImageView

I am trying to create an animation effect where an image who's alpha value is 0.5 (approximately) at start changes to 1 gradually, but not like the conventional effect. 我正在尝试创建一个动画效果,其中开始时alpha值为0.5(大约)的图像逐渐变为1,但不像传统效果那样。 Here is what I am looking for. 这是我正在寻找的。

original Image. 原始图像。

在此输入图像描述

after some time 一段时间后

在此输入图像描述

after some more time 过了一段时间

在此输入图像描述

and at the end 最后

在此输入图像描述

So basically if the alpha value of part of the image can be reduced than I can show the animation of filling up a glass or something close to it. 所以基本上如果图像的一部分的alpha值可以减少,我可以显示填充玻璃或接近它的东西的动画。
I don't know if this can be done at all or if it's possible with core graphics or core animation. 我不知道这是否可以完成,或者是否可以使用核心图形或核心动画。

PS They are made in photoshop. PS他们是在photoshop中制作的。
Does anyone have any thoughts about what should be done to achieve this kind of animation? 有没有人对应该采取什么措施来实现这种动画?

You can add view above and change it's frame. 您可以在上方添加视图并更改其框架。 The best way. 最好的方法。

If you're using images, then sunclass UIView and implement 如果你正在使用图像,那么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];
}

I'd suggest the following: 我建议如下:

  1. Create the UIImageView that shows the image in the view. 创建在视图中显示图像的UIImageView。
  2. Create a new UIView with a background color and transparency: 创建一个具有背景颜色和透明度的新UIView:

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

     [UIView animateWithDuration:1.0 animations:^{ transpView.frame = CGRectOffset(transpView.frame, 0, -tranpsView.frame.size.height); }]; 

Don't forget to release the tranpsView after you are done. 完成后不要忘记释放tranpsView Possibly with the completed block. 可能与completed块。

To get the "glass filling up" type of animation, just change the frame of of the tranpsView accordingly. 要获得“玻璃填充”类型的动画,只需相应地更改tranpsViewframe The tranpsView can be any UIView subclass (like a texture of liquid or something). tranpsView可以是任何UIView子类(如液体或其他东西的纹理)。

Hope that helps.! 希望有所帮助。!

Here's yet another idea. 这是另一个想法。

Set the background of your UIView *view to the image.png you want with alpha = 0.5. 将您的UIView *view的背景设置为alpha = 0.5所需的image.png Let

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

Create another UIView *glassView also with alpha = 0.5, and add it as a subview: 使用alpha = 0.5创建另一个UIView *glassView ,并将其添加为子视图:

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

Create a UIImageView *glassImageView and set its image to image.png as well, and add it as a subview of glassView : 创建一个UIImageView *glassImageView并将其image设置为image.png ,并将其添加为glassView的子视图:

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

Now set up the animation to increase the height and alpha of glassView while maintaining the size of imageGlassView , something like this: 现在设置了动画,以增加高度和α glassView ,同时保持规模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];        

To be fair, I haven't tested this out, but something along these lines seems like it may work. 公平地说,我没有对此进行过测试,但是沿着这些方向的某些东西似乎可能会起作用。 Then again, maybe not. 然后,也许不是。 If you found a perfect solution, please post it back. 如果您找到了完美的解决方案,请将其发回。 I'd love to know how to do this! 我很想知道怎么做!

Well I know this question is quite old and I am the one who asked it, but today I was experimenting with something similar and found this simple solution to my own question which was asked 10 months ago. 嗯,我知道这个问题已经很老了,而且我是那个问过它的人,但是今天我正在尝试类似的东西,并且发现这个简单的解决方案来解决我10个月前提出的问题。

I was working on a project which requires a image to look like its loading from completely white which is 0% progress to completely red image which is 100% progress. 我正在开发一个项目,要求图像看起来像是从完全白色的加载,0%进展到完全红色图像,这是100%的进展。

I thought of adding 2 imageViews containing the images on top of each other and set the content mode of the top imageview containing image with red color to bottom so that when you reduce the size of imageview the image appears to be loading and it worked. 我想添加2个包含图像的imageViews并将包含图像的顶部imageview的内容模式设置为底部,这样当你缩小imageview的大小时,图像似乎正在加载并且它起作用。 With some animation blocks the image appears to be loading. 对于一些动画块,图像似乎正在加载。 Then I thought of this question that I had asked so long and then came up with the solution to my own problem. 然后我想到了这个问题,我问了很久,然后提出了解决我自己问题的方法。

So the solution to this question will be that if you add 2 imageViews on top of each other and then reduce the alpha value one that is behind and reduce the height to zero and then gradually increase the height and change the y coordinate of the image view that has alpha value 1 then It will appear to be loading as asked in this question. 所以这个问题的解决方案是,如果你在彼此的顶部添加2个imageViews,然后减少后面的alpha值并将高度降低到零,然后逐渐增加高度并更改图像视图的y坐标具有alpha值1然后它似乎按照此问题中的要求加载。

I have used these two functions to get the illusion of loading image. 我已经使用这两个函数来获得加载图像的错觉。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM