ios iPhone:UIImageView 淡入淡出 - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5632391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:33:03  来源:igfitidea点击:

iPhone: UIImageView Fades in - Howto?

iphoneiosipaduiimageviewxamarin.ios

提问by Ian Vink

I have an UIImageViewthat I want to fade in.

我有一个UIImageView我想淡入的。

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

有没有办法在底层启用它UIViewController

I have translated the simplest answer, though they all work, C# .NETfor the MonoTouchusers:

我已经C# .NETMonoTouch用户翻译了最简单的答案,尽管它们都有效:

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

回答by visakh7

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

最初将图像视图的 alpha 设置为 0 imageView.alpha = 0;

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

}

回答by Sabobin

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;
}];

回答by Jhaliya

Use below in your UIViewController

在您的以下使用 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"];

回答by Milad

this simple code:

这个简单的代码:

[UIView animateWithDuration:5.0 animations:^{
        theImage.alpha = 1.0;
    }];

the UIImageis in the view and is connected via an IBOutletyou can also, set the alpha from the utility panel.

UIImage是在视图中并且经由连接IBOutlet你也可以,设置从公用面板阿尔法。

回答by Tibidabo

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

我会使用 UIView 的 +transitionWithView:duration:options:animations:completion: 它非常高效和强大。

[UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    imgView.image = [UIImage imageNamed:@"MyImage"];
} completion:nil];

回答by Haroldo Gondim

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.yourComponentcan be a UIView, UIImageView, UIButtonor any other component.

self.yourComponent可以是UIViewUIImageViewUIButton或任何其它部件。

回答by Vivekanandan

Swift version

迅捷版

func fadeIn(){
    UIView.beginAnimations("fade in", context: nil);
    UIView.setAnimationDuration(1.0);
    imageView.alpha = 1.0;
    UIView.commitAnimations();
}