objective-c 试图淡入 UIView 没有成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2131694/
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
Trying to fade in a UIView without success
提问by Ricky
I am trying to fade in a UIView as a subview of my main view. The UIView I am trying to fade in has the dimensions of 320x55.
我试图淡入 UIView 作为我的主视图的子视图。我试图淡入的 UIView 的尺寸为 320x55。
I setup the view and a timer;
我设置了视图和计时器;
secondView.frame = CGRectMake(0, 361, 320, 55);
secondView.alpha = 0.0;
[self.view addSubview:secondView];
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(fadeView) userInfo:NO repeats:NO];
The timer triggers the following code;
定时器触发以下代码;
secondView.alpha = 1.0;
CABasicAnimation *fadeInAnimation;
fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.5;
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[fadeInAnimation setDelegate:self];
[secondView.layer addAnimation:fadeInAnimation forKey:@"animateOpacity"];
My secondView is connected in Interface Builder and responds to other messages but I can't see anything happening on screen.
我的 secondView 连接在 Interface Builder 中并响应其他消息,但我看不到屏幕上发生的任何事情。
Can anyone please help me figure out what's going on here?
谁能帮我弄清楚这里发生了什么?
Thanks, Ricky.
谢谢,瑞奇。
In reply to a following recommendation:
回复以下建议:
I'm a bit unsure here. Initially I put this code in (because I see secondView as an instance of UIView?):
我在这里有点不确定。最初我将这段代码放入(因为我将 secondView 视为 UIView 的一个实例?):
[secondView beginAnimations:nil context:NULL];
[secondView setAnimationDuration:0.5];
[secondView setAlpha:1.0];
[secondView commitAnimations];
I then tried your suggestion which didn't produce warnings or errors, but it still does brings nothing to the surface:
然后我尝试了你的建议,它没有产生警告或错误,但它仍然没有带来任何表面:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[secondView setAlpha:1.0];
[UIView commitAnimations];
Thanks! Ricky.
谢谢!瑞奇。
回答by kubi
You should be able to do this a bit simpler. Have you tried something like this?
你应该能够更简单地做到这一点。你有没有尝试过这样的事情?
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[secondView setAlpha:1.0];
[UIView commitAnimations];
回答by steveb
it seems to me that one thing you're missing is that your view may already have an alpha of 1.0. make sure the alpha is 0 (or whatever you desire it to be) prior to the animation call.
在我看来,您缺少的一件事是您的视图可能已经具有 1.0 的 alpha。在动画调用之前,确保 alpha 为 0(或任何你想要的值)。
i prefer to use block animations for this. it's cleaner and more self-contained.
我更喜欢为此使用块动画。它更干净,更独立。
secondView.alpha = 0.0f;
[UIView animateWithDuration:1.5 animations:^() {
secondView.alpha = 1.0f;
}];
回答by kennytm
This won't answer your question if you insist on using CoreAnimations, but for iPhoneOS it's much easier to use animation blocks for UIView animations.
如果您坚持使用 CoreAnimations,这不会回答您的问题,但对于 iPhoneOS,将动画块用于 UIView 动画要容易得多。
secondView.alpha = 0.0f;
[UIView beginAnimations:@"fadeInSecondView" context:NULL];
[UIView setAnimationDuration:1.5];
secondView.alpha = 1.0f;
[UIView commitAnimations];
Also, you can invoke a delegate in delayed time with
此外,您可以使用延迟时间调用委托
[self performSelector:@selector(fadeView) withObject:nil afterDelay:0.5];
回答by NaXir
Here is also a good alternative with many options and easy to use.
这也是一个很好的选择,有很多选择并且易于使用。
[secondViewController.view setAlpha:0.0];
[UIView animateWithDuration:1.5
delay:0.0
options:UIViewAnimationOptionCurveEaseIn // See other options
animations:^{
[secondViewController.view setAlpha:1.0];
}
completion:^(BOOL finished) {
// Completion Block
}];

