开发者

IPHONE - fade in and fade out of a UIImageView with different times

I would like to do a f开发者_开发问答ade in and out of a UIImageView using different times, let's say, using the following parameters:

  • t = 0 ... UIImageView's alpha = 0
  • t = 0.5s ... UIImageView's alpha = 0.7
  • t = 0.7s ... UIImageView's alpha = 0

Is this possible to do with CAAnimation or other method? How can that be done?

thanks for any help!


if (imgDefault.alpha == 0.0) {
    CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration: 3.0];
        [UIView setAnimationDelegate: self];
        imgDefault.alpha = 1.0;
        [UIView commitAnimations];
}
else {
    CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration: 3.0];
        [UIView setAnimationDelegate: self];
        imgDefault.alpha = 0.0;
        [UIView commitAnimations];
}

hope it helps


You should probably look into CAKeyframeAnimation. It'll let you set values for multiple time points.


UIView has a setAnimationDidStopSelector: method which you can use. Simply setup your fade in animation using a beginAnimations block and set the didStop selector to another method which contains only the fade out animation block. Each of these animation blocks can have different animation durations.

Something like this:

    [UIView beginAnimations:next context:context];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(fadeOut:finished:context:)];
    myView.alpha = 0.7;
    [UIView commitAnimations];

-(void)fadeOut:(NSString*)animationID finished:(BOOL)finished context:(void*)context  {
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationDuration:0.2];
    myView.alpha = 0.0;
    [UIView commitAnimations];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜