iPhone: UIImageView Fades in - Howto?

2020-05-19 08:20发布

I have an UIImageView that I want to fade in.

Is there a way to enable that on an underlying UIViewController?

I have translated the simplest answer, though they all work, C# .NET for the MonoTouch users:

public override void ViewDidAppear (bool animated)
{
    base.ViewDidAppear (animated);
    UIView.BeginAnimations ("fade in");
    UIView.SetAnimationDuration (1);
    imageView.Alpha = 1;
    UIView.CommitAnimations ();
}

7条回答
相关推荐>>
2楼-- · 2020-05-19 08:35

I would use UIView's +transitionWithView:duration:options:animations:completion: It is very efficient and powerful.

[UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    imgView.image = [UIImage imageNamed:@"MyImage"];
} completion:nil];
查看更多
一夜七次
3楼-- · 2020-05-19 08:40

You can use this code:

Fade In Animation:

self.yourComponent.alpha = 0.0f;
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDuration:1.0]; // Time in seconds
self.yourComponent.alpha = 1.0f;

Fade Out Animation:

self.yourComponent.alpha = 1.0f;
[UIView beginAnimations:@"fadeOut" context:nil];
[UIView setAnimationDuration:1.0]; // Time in seconds
self.yourComponent.alpha = 0.0f;

self.yourComponent can be a UIView, UIImageView, UIButton or any other component.

查看更多
Emotional °昔
4楼-- · 2020-05-19 08:47

Initially set the alpha of your imageview as 0 as imageView.alpha = 0;

- (void)fadeInImage 
{
[UIView beginAnimations:@"fade in" context:nil];
    [UIView setAnimationDuration:1.0];
    imageView.alpha = 1.0;
    [UIView commitAnimations];

}
查看更多
够拽才男人
5楼-- · 2020-05-19 08:52

Change the animation duration to what ever length you want.

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
myImageView.center = CGPointMake(100, 100);
myImageView.alpha = 0.0;
[self.view addSubview:myImageView];
[UIView animateWithDuration:5.0 animations:^{
     myImageView.alpha = 1.0;
}];
查看更多
我命由我不由天
6楼-- · 2020-05-19 08:54

Use below in your UIViewController

// add the image view
[self.view addSubview:myImageView];
// set up a transition animation
CATransition *animate = [CATransition animation];
[animate setDuration:self.animationDelay];
[animate setType:kCATransitionPush];
[animate setSubtype:kCATransitionFade];
[animate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[self layer] addAnimation:animate forKey:@"fade in"];
查看更多
做自己的国王
7楼-- · 2020-05-19 08:54

Swift version

func fadeIn(){
    UIView.beginAnimations("fade in", context: nil);
    UIView.setAnimationDuration(1.0);
    imageView.alpha = 1.0;
    UIView.commitAnimations();
}
查看更多
登录 后发表回答