简体   繁体   中英

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. 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.
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.
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

-(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.
  2. Create a new UIView with a background color and transparency:

     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. Possibly with the completed block.

To get the "glass filling up" type of animation, just change the frame of of the tranpsView accordingly. The tranpsView can be any UIView subclass (like a texture of liquid or something).

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. 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:

[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 :

[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:

[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.

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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