gpt4 book ai didi

ios - 以编程方式删除 subview

转载 作者:行者123 更新时间:2023-11-28 19:13:04 25 4
gpt4 key购买 nike

我在 appdelegate 的 didReceiveRemoteNotification: 方法中得到了这段代码

我想知道如何使用延迟删除此 subview ,假设 subview 显示来自推送通知的信息,但我希望它们在 3.5 秒后消失(延迟 2)

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

最佳答案

您可以使用 UIView 的 removeFromSuperview 实例方法。在为所有窗口设置标签添加 View 时。并在 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];
}

关于ios - 以编程方式删除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com