状态消息损坏了我的iPhone应用程序中的主窗口



在我的 iOS 应用程序中,按下按钮时会启动一个事件,并在屏幕底部显示一条状态消息,通知用户操作成功或失败。

状态消息显示并消失,并使用以下代码。

这个显示消息

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

奇怪的是,显示消息并在两秒钟后消失,但是主窗口失去了功能,例如复制和粘贴功能。

你觉得我的方法有问题吗?有没有某种方法,调用"动画"代码?

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

  • 这是因为您使用UIWindow来显示视图,这不是一个好主意(不建议自己创建UIWindows,如文档的"概述"段落中所述。
  • 此外,您将窗口设置为键窗口,但在隐藏它时不会在最后恢复标准窗口,因此以后会出现事件问题。
  • 最后,您对动画使用过时的 API,由于 iOS4 已经发布(我猜您没有为 iOS3 或更早版本编写代码),因此已弃用,您实际上应该使用现在已经引入的新 API 3 个 iOS 版本。此外,它将避免为此创建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];
    }];
}

最新更新