如何使图像在iOS应用程序中显示和消失?

时间:2012-09-12 11:32:26

标签: objective-c

我正在尝试让我的应用在按下按钮时在屏幕中间显示图像(PNG),然后在之后使其淡出几秒钟。我怎么能这样做?它也是一个小png,所以我怎么能让它以原始尺寸显示而不是伸展它以适应整个屏幕? 非常感谢任何提示,建议或答案!

此外,我是这个网站的新用户,所以请你给我提示或帮我改进这个问题,因为有些人认为它不完整。谢谢大家的慷慨解答! :)

3 个答案:

答案 0 :(得分:10)

使用UIImageView初始化UIImage

// Assuming MyImage.png is part of the project's resources.
UIImage *image = [UIImage imageNamed:@"MyImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add imageView to a parent view here.
[UIView animateWithDuration:0.2f delay:3.0f options:0 
                 animations:^{imageView.alpha = 0.0;} 
                 completion:^{[imageView removeFromSuperview];}];

答案 1 :(得分:3)

 NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"myImage.png"], nil]; 

    [NSTimer scheduledTimerWithTimeInterval:.75 target:self selector:@selector(crossfade) userInfo:nil repeats:YES];

    mainImageView.animationImages = animationArray;                
    mainImageView.animationDuration = 4.5; //mainImageView is instance of UIImageView
    mainImageView.animationRepeatCount = 0;
    [mainImageView startAnimating];

    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
    crossFade.autoreverses = YES;
    crossFade.repeatCount = 1;
    crossFade.duration = 1.0;

和目标方法:

- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // user dependent transition acn be set here
    mainImageView.alpha = !mainImageView.alpha;
    [UIView commitAnimations];
}

答案 2 :(得分:3)

以下是按下按钮时显示几秒钟图像的代码: 在您的按钮操作中添加以下代码:

imageView.image=yourImage;
 [self performSelector:@selector(waitAndGo) withObject:nil afterDelay:5];

这是waitAndGo的实现:

-(void)waitAndGo
{
    imageView.hidden=YES;
}