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
iPhone: UIImageView Fades in - Howto?
提问by Ian Vink
I have an UIImageView
that 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# .NET
for the MonoTouch
users:
我已经C# .NET
为MonoTouch
用户翻译了最简单的答案,尽管它们都有效:
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 UIImage
is in the view and is connected via an IBOutlet
you 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.yourComponent
can be a UIView
, UIImageView
, UIButton
or any other component.
self.yourComponent
可以是UIView
,UIImageView
,UIButton
或任何其它部件。
回答by Vivekanandan
Swift version
迅捷版
func fadeIn(){
UIView.beginAnimations("fade in", context: nil);
UIView.setAnimationDuration(1.0);
imageView.alpha = 1.0;
UIView.commitAnimations();
}