Objective c 试图在UIView中淡出却没有成功

Objective c 试图在UIView中淡出却没有成功,objective-c,animation,uiview,Objective C,Animation,Uiview,我正在尝试淡入UIView作为主视图的子视图。我尝试淡入的UIView的尺寸为320x55 我设置了视图和计时器 secondView.frame = CGRectMake(0, 361, 320, 55); secondView.alpha = 0.0; [self.view addSubview:secondView]; [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(fadeView) us

我正在尝试淡入UIView作为主视图的子视图。我尝试淡入的UIView的尺寸为320x55

我设置了视图和计时器

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];
定时器触发以下代码

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"];
我的secondView在Interface Builder中连接并响应其他消息,但我在屏幕上看不到任何事情

有谁能帮我弄清楚这是怎么回事吗

谢谢, 瑞奇


针对以下建议:

我有点不确定。最初,我将此代码放入(因为我将secondView视为UIView的实例?)

然后我尝试了你的建议,虽然没有产生警告或错误,但它仍然没有让任何东西浮出水面:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[secondView setAlpha:1.0]; 
[UIView commitAnimations]; 
谢谢!瑞奇


如果你坚持使用CoreAnimations,这并不能回答你的问题,但对于iPhoneOS来说,在UIView动画中使用动画块要容易得多

secondView.alpha = 0.0f;
[UIView beginAnimations:@"fadeInSecondView" context:NULL];
[UIView setAnimationDuration:1.5];
secondView.alpha = 1.0f;
[UIView commitAnimations];

此外,您还可以在延迟时间内使用

[self performSelector:@selector(fadeView) withObject:nil afterDelay:0.5];

你应该可以做得简单一点。你试过这样的东西吗

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[secondView setAlpha:1.0];
[UIView commitAnimations];

在我看来,您缺少的一点是,您的视图可能已经有了1.0的alpha值。在调用动画之前,确保alpha为0(或您希望的任何值)

我更喜欢使用块动画。它更干净,更独立

secondView.alpha = 0.0f;
[UIView animateWithDuration:1.5 animations:^() {
    secondView.alpha = 1.0f;
}];

这也是一个很好的选择,有很多选择,而且使用方便

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

通过调用UIView setAnimationDelay,您可以一起摆脱计时器。e、 g.[UIView setAnimationDelay:0.5]该代码生成3条警告,都表示UIView可能不会响应消息。它还会使应用程序崩溃。为什么会这样?@Ricky:真奇怪。你正在使用iPhone SDK吗?你能发布周围的代码吗?当我这样做的时候,它编译得很好。是的,我正在使用iphonesdk。有关格式化代码,请参阅我编辑的原始帖子。不会为我生成任何警告。也许你的第二个视图不是UIView?具体地说,是动画自己的线程,它是主线程。动画将随时间间隔(动画持续时间)进行更改。另一方面,请记住从主线程调用“UIView animateWithDur…”方法。因此,如果此调用是从main以外的线程发出的,则使用dispatch_asynch(dispatch_get_main_queue(),^{[UIView animateWithDur…]}将其包装起来+1对于
,您的视图可能已经具有1.0的alpha。在这种情况下,从1.0到1.0“设置动画”,动画将立即结束,而不是在指定的持续时间之后。确保确实要对动画进行更改。@GökhanBarışAker关于线程的观点正确无误。我的大部分积木都有自己的线索,我说得太快了。我已经更新了帖子。谢谢你指出这一点。
  [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
        }];