开发者

Cannot determine UIImage opacity attribute

开发者 https://www.devze.com 2023-03-17 09:33 出处:网络
I need to have a UIImage fade onto the screen when a button is pressed. The button code is finished and works, but since I\'m new to IOS development开发者_StackOverflow, I am uncertain of the UIImage

I need to have a UIImage fade onto the screen when a button is pressed. The button code is finished and works, but since I'm new to IOS development开发者_StackOverflow, I am uncertain of the UIImage attributes to accomplish this.


What you want is a UIImageView - and you want the alpha property from UIView

And you will likely also want to read up on [UIView animateWithDuration:…].


You'd probably want to use CoreAnimation. UIImage doesn't have an opacity property because it doesn't have any display logic, but UIImageView does and has because it's a subclass of UIView.

To put it another way, a UIImage is just an image. If you want to change the opacity with which it is drawn then you want to talk to the actor that does the drawing, which is (normally) UIImageView.

So, e.g.

// set the current alpha to 0.0f
someImageView.alpha = 0.0f;

// use the UIView convenience methods to create a CoreAnimation block. Have the
// animation just be to move the alpha up to 1.0f. Because we don't specify any
// other adjustments, we'll get the default 0.2 seconds and ease in/ease out
// curve for alpha against time
[UIView beginAnimations:nil context:nil];
    someImageview.alpha = 1.0f;
[UIView commitAnimations];

// the animation will now run autonomously

You could do something like that in response to the button press and you should achieve what you're attempting. CoreAnimation can change a whole bunch of properties automatically, not just alpha, and can do much more complicated tasks than just transitioning from one fixed state to another, but UIView wraps up and significantly simplifies the most common use.


This is how I fixed the opacity issue and displaying the image...there's more to it (button click to SEL method) but did want to display that much.

-(IBAction) seeImage
{
CGContextRef imageContext = UIGraphicsGetCurrentContext();
wisdomView.alpha = 0.0;

[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];

SEL methodSelector = @selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector      userInfo:nil repeats:NO];

}

////This is the method that hides the graphic choosen by randNum and make it disappear at the top of the screen
-(IBAction) hideImage
{

CGContextRef imageContext = UIGraphicsGetCurrentContext();
wisdomView.alpha = 1;

[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:6];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 0.0;
[UIView commitAnimations];

}

it works! If anyone has any ideas on how to make it better...let me know! Thanks all!

0

精彩评论

暂无评论...
验证码 换一张
取 消