0

私の 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];
}

奇妙なことに、メッセージが表示されて 2 秒後に消えますが、メイン ウィンドウではコピー アンド ペースト機能などの機能が失われます。

私の方法に何か問題がありますか?「アニメーション」コードを呼び出す特定の方法はありますか?

4

2 に答える 2

0

あなたが書いているので、 self.view.window がすべてのユーザーインタラクションを再び処理できるように[messageViewWindow makeKeyAndVisible];書き込む必要があります。[self.view.window makeKeyAndVisible]hideMessage

于 2012-10-13T15:04:06.703 に答える
0
  • これはUIWindowビューを表示するために a を使用しているためです。これは良い考えではありません (UIWindowsドキュメントの「概要」の段落で説明されているように、自分で作成することはお勧めしません。
  • さらに、ウィンドウをキーウィンドウにしますが、非表示にしたときに最後に標準ウィンドウを復元しないため、後でイベントの問題が発生します。
  • そして最後に、iOS4 がリリースされてから非推奨になっている古い API をアニメーションに使用している場合 (そして、iOS3 以前ではコーディングしていないと思います)、3 つの iOS バージョンより前に導入された新しい API を実際に使用する必要があります。また、そのための を作成するためのオーバーヘッドを回避できます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];
    }];
}
于 2012-10-13T15:22:23.320 に答える