0

appdelegate の didReceiveRemoteNotification: メソッド内でこのコードを取得しました

遅延を使用してこのサブビューを削除する方法を知りたいのですが、サブビューはプッシュ通知から情報を表示すると想定されていますが、3.5 秒後にそれらを非表示にしたいです (delay2)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);

    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
    // Create the UILabel instance

    CGRect myFrame = CGRectMake(0, 20, 320, 50);
    UIView *myView = [[UIView alloc] initWithFrame:myFrame];
    myView.backgroundColor = [UIColor blueColor];
    [self.window addSubview:myView];
    [myView release];

    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)];
    [aLabel setText:alert];
    [self.window addSubview:aLabel];
    [aLabel release];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"];

    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
    //SONAR//
    [self performSelector:@selector(delay2) withObject:nil afterDelay:3.5];
}

-(void)delay2 {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"];

    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

    [theAudio play];
}
4

1 に答える 1

4

UIView の removeFromSuperview インスタンス メソッドを使用できます。ウィンドウにビューを追加しながら、すべてにタグを設定します。そして、delay2 メソッドでは、tag & removeFromSuperview メソッドを使用してそれらを削除します。

CGRect myFrame = CGRectMake(0, 20, 320, 50);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor blueColor];
[myView setTag:999];
[self.window addSubview:myView];
[myView release];

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)];
[aLabel setText:alert];
[aLabel setTag:999];
[self.window addSubview:aLabel];
[aLabel release];


-(void)delay2 {

for(UIView *subView in window.subviews)
{
    if(subView.tag == 999)
        [subView removeFromSuperview];
}

}

于 2012-12-23T07:19:27.503 に答える