2012-03-22 55 views
0

我找不到为什么这个UIView在调用方法时无法正确显示。它在另一个视图控制器中工作。当我在RootViewController中尝试它时,NSLog输出工作正常,但没有视图动画。我想知道它是否显示,但隐藏的看法。任何帮助都会很棒。当一个方法被调用时,UIView无法正确显示

- (IBAction)showView:(id)sender{ 

    NSLog(@"showView button pressed"); 

    [self.view addSubview: menuView]; 
    [self.view bringSubviewToFront:menuView]; 

    CGRect rect = menuView.frame; 
    rect.origin.x = 60; 
    rect.origin.y = -400; 
    menuView.frame = rect; 
    [UIView beginAnimations:@"ShowView" context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:0.5]; 
    rect.origin.y = 65; 
    menuView.frame = rect; 

    [UIView commitAnimations]; 

} 
+0

当你调用'NSLog(@“View:%@”,self.view)时会记录什么;''低于其他NSLog? – JNK 2012-03-22 16:58:50

+0

出来的脚本是:查看:> – hanumanDev 2012-03-23 10:28:47

回答

2

这个工作在你的RootViewController的,那么你复制showView:代码到一个新的视图控制器和它doesn没有工作。其余部分与@ kl94进行对话。 menuView已被声明,但未在新视图控制器中初始化。我想如果你回到RootViewController工作的位置,你会发现menuView被初始化的代码:... menuView = [[SomeViewClass alloc] init .....代码需要被复制到新的视图控制器。一个体面的地方放在viewDidLoad方法。

我同意@Amiramix的块动画是要走的路。我建议代码如下:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // code copied from RootViewController that initializes menuView 
    // probably, the frame is set so that the menu is positioned off the visible part of the view 
    [self.view addSubview:menuView]; 
} 

- (IBAction)showView:(id)sender{ 

    NSLog(@"showView button pressed, menu view is %@", menuView); 

    [UIView animateWithDuration:0.5 animations:^{ 
     menuView.frame = CGRectMake(/* the rect where you want the menu to end up */); 
    }]; 
} 
2

你有没有很好的链接你的menuView,这是我认为,IB视图?
尝试做一个简单的NSLog(@"%@", menuView);并告诉我们它显示的是什么。

+1

它返回(null)。我猜我的menuView没有链接。我没有使用IB。我将如何去编程链接menuView?感谢您的帮助 – hanumanDev 2012-03-23 10:34:23

+0

这意味着您使用的视图不存在,您在哪里以及如何声明了它? – klefevre 2012-03-23 15:04:43

1

当您添加subview它会自动放置在所有其他视图的前面,因此无需拨打bringSubviewToFront。另外,您需要在beginAnimiation - commitAnimation块内设置帧。最后,请考虑即使只用于调试您的问题,如起见使用新animateWithDuration方法:

void (^block)(void) = ^{ 
    [self->textView setFrame:CGRectMake(0, 0, self.bounds.size.width, keyboardView.origin.y)]; 
}; 

[UIView animateWithDuration:0.2 animations:block];