3 Replies Latest reply: Mar 9, 2009 1:14 PM by nicksayes
nicksayes Level 1 Level 1 (0 points)
I'm a newbie iPhone developer going very well, almost finished my first app minus this one issue which I am a little embarrassed to be asking but...

I have a UIImageView that I have animated to fade (alpha value) over the period of 60s. After the animation a UIButton displays. In addition by tapping you can force the alpha value of the UIImageView down as to make the animation appear faster. But I am having trouble determining when the alpha value of my UIImageView is below zero - so that I can display the button - even though the animation is still running. Here is my code:

- (void)fadeTheImageView
{
[fadeImageView setAlpha:1.0];

[UIView beginAnimations:@"theAnimation" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(yourAnimationHasFinished:finished:context :)];
[UIView setAnimationDuration:10];
[fadeImageView setAlpha:0.0];
[UIView commitAnimations];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ( fadeImageView.alpha <= 0.0 ) {
[saveButton setHidden:NO];
}
[fadeImageView setAlpha:(fadeImageView.alpha - 0.1)];
}

- (void)yourAnimationHasFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context;
{
[saveButton setHidden:NO];
}

The error is with the if ( fadeImageView.alpha <= 0.0 ) {
incompatible variable types.
I've tried everything.

Any help would be appreciated

iMac, Mac OS X (10.5.6)
  • RayNewbie Level 5 Level 5 (6,810 points)
    Hi Nick. I've only tested touchesEnded so far, since you said that was the only problem. I haven't been able to reproduce the "incompatible variable types" error, so don't know where that's coming from. However I did notice one thing that might be relevant: In my test runs ( fadeImageView.alpha <= 0.0 ) never becomes YES, and on second look I don't see how we can expect that since a view no longer catches touches when it's alpha goes to zero (I think you need alpha >= 0.02 to catch touch events).

    Maybe there's more going on here that I'd see if I tested touchesEnded with the rest of your code and I'll try to do that tomorrow. Btw, it would help if you formatted your code so it could be pasted into a test bed verbatim. Do that like this:


    [fadeImageView setAlpha:(fadeImageView.alpha - 0.1)];


    The above lines produce:

    [fadeImageView setAlpha:(fadeImageView.alpha - 0.1)];
  • nicksayes Level 1 Level 1 (0 points)
    Thanks

    Its news to me that there is a limit of 0.02 alpha on touches - and strangely I'm not getting the incompatible variable types error any more. The alpha limit on touches doesn't fix the issue though.

    I think the code

    if (fadeImageView.alpha <= 0.3)

    is simply ignored as the button (which is programmed to become visible with the condition above) becomes visible with 1 tap ... when the alpha is clearly higher than 0.3?

    Hmmm?
  • nicksayes Level 1 Level 1 (0 points)
    I've worked around the problem.
    What I have done is to animate the alpha of the button along side the UIImageView.
    So no need to rely on that conditional.


    - (void)fadeTheImageView
    {
    UIImage *fadeImage = [UIImage imageNamed:@"Base.png"];
    fadeImageView.image = fadeImage;
    [fadeImageView setAlpha:1.0];
    [saveButton setHidden:NO];
    [saveButton setAlpha:-10.0];

    [UIButton beginAnimations:@"saveButton" context:NULL];
    [UIButton setAnimationDuration:30];
    [saveButton setAlpha:1.0];
    [UIButton commitAnimations];

    [UIView beginAnimations:@"theAnimation" context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(yourAnimationHasFinished:finished:context:)];
    [UIView setAnimationDuration:30];
    [fadeImageView setAlpha:0.0];
    [UIView commitAnimations];
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [saveButton setAlpha:(saveButton.alpha + 1.0)];
    [fadeImageView setAlpha:(fadeImageView.alpha - 0.1)];
    }

    - (void)yourAnimationHasFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context;
    {
    [saveButton setAlpha:1.0];
    }