2012-10-13 30 views
0

破坏主窗口在我的iOS应用程序,并在屏幕的底部出现一条状态消息通知用户一个成功或失败的操作。状态消息在我的iPhone应用程序

状态消息出现,并用下面的代码消失。

这一个显示消息

- (void) showMessage:(NSString*)text 
{ 
     CGRect frame = [[self view] frame]; 

     if(statusView == nil) 
     { 
      messageViewWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, frame.size.height-29, 
                 frame.size.width, 0)]; 


      statusView = [[StatusMessageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 0) 
                  Text:text 
               andShowIndicator:NO]; 
      [messageViewWindow addSubview:statusView]; 
      [messageViewWindow makeKeyAndVisible]; 
     } 

     UILabel *label = [statusView getTextLabel]; 
     UIImageView *imageView = [statusView getBackImageView]; 

     CGSize stringSize = [text sizeWithFont:[Constants getLabelFont]]; 
     int xCoordinate = (messageViewWindow.frame.size.width - stringSize.width)/2; 

     [UIView beginAnimations:nil context:nil]; 
     messageViewWindow.frame = CGRectMake(0, frame.size.height-59, frame.size.width, 30); 
     statusView.frame = CGRectMake(0, 0, frame.size.width, 30); 
     label.frame = CGRectMake(xCoordinate,5,stringSize.width,20); 
     imageView.frame = CGRectMake(0, 0, frame.size.width, 30); 
     [UIView commitAnimations]; 

     [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(hideMessage) userInfo:nil repeats:NO]; 

} 

和这一个隐藏消息

- (void) hideMessage 
{ 
    UILabel *label = [statusView getTextLabel]; 
    UIImageView *imageView = [statusView getBackImageView]; 

    CGRect labelFrame = label.frame; 

    [UIView beginAnimations:nil context:nil]; 
    messageViewWindow.frame = CGRectMake(0, [UIScreen mainScreen].applicationFrame.size.height-29, [UIScreen mainScreen].applicationFrame.size.width, 0); 
    statusView.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0); 
    label.frame = CGRectMake(labelFrame.origin.x, 0, labelFrame.origin.y, 0); 
    imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0); 
    [UIView commitAnimations]; 
} 

奇怪的是,被示出的消息两秒自败,但是主窗口失去功能后,就像复制和粘贴功能一样。

你看到我的方法出问题了吗?有没有什么特定的方法,称为“动画”代码?

+0

为什么是奇怪吗?您正在设置计时器以在显示它两秒后隐藏它。 – Martol1ni

+0

@ Martol1ni状态消息显示2秒钟,之后将被删除。这很好。删除后UIView有问题。 – cateof

回答

0

由于您正在编写[messageViewWindow makeKeyAndVisible];,因此您必须在hideMessage中编写[self.view.window makeKeyAndVisible]以让self.view.window再次处理所有用户交互。

0
  • 这是因为你使用UIWindow,以显示你的观点,这是不是一个好主意(不建议创建自己UIWindows,如在文档的“概述”的段落解释。
  • 此外,你让你的窗口keywindow但最后,当你把它隐藏,从而与您的事件后面的问题不恢复标准窗口。
  • 而在去年,您使用过时的API动画,因为它被废弃iOS4已经出来了(我猜你不会为iOS3或更早的版本编写代码),你应该真的使用现在已经引入3个iOS版本的新API,此外,它将避免overh ead为此创建NSTimer

所以,你的代码可以成为:

- (void) showMessage:(NSString*)text 
{ 
    CGSize sz = self.view.window.frame.size; 
    CGRect hiddenFrame = CGRectMake(0, sz.height-29, sz.width, 0); 

    if(statusView == nil) 
    { 
     statusView = [[StatusMessageView alloc] initWithFrame:hiddenFrame 
                 text:text 
              andShowIndicator:NO]; 
     [self.window addSubview:statusView]; 
    } 

    UILabel *label = [statusView getTextLabel]; 
    UIImageView *imageView = [statusView getBackImageView]; 

    CGSize stringSize = [text sizeWithFont:[Constants getLabelFont]]; 
    int xCoordinate = (sz.width - stringSize.width)/2; 
    label.frame = CGRectMake(xCoordinate,5,stringSize.width,20); 
    imageView.frame = CGRectMake(0, 0, frame.size.width, 30); 

    [UIView animateWithDuration:1.f animations:^{ 
     statusView.frame = CGRectMake(0, sz.height-59, frame.size.width, 30); 
    } completion:^{ 
     // When show animation is done, schedule the start of the hide animation with a delay of 2 seconds 
     [UIView animateWithDuration:1.f delay:2.f options:0 animation:^{ 
      statusView.frame = hiddenFrame; 
     } completion:nil]; 
    }]; 
} 
+0

小挑剔:虽然它们的使用气馁,'beginAnimations'和'commitAnimations'未弃用(至少iOS 6中的)。从文档“在iOS 4.0及更高版本中不鼓励使用此方法,您应该使用基于块的动画方法来指定您的动画。” – idz

相关问题