gpt4 book ai didi

iphone - 在 View 中添加/删除 View 时出现问题

转载 作者:行者123 更新时间:2023-12-03 19:54:16 24 4
gpt4 key购买 nike

我正在尝试向下面的 View 添加/删除 View (_countdown),添加 View 没有问题。但当我试图删除它时,什么也没有发生。我所有的 NSLog() 都被调用,所以一切都应该正常。但是当它到达 -(void)removeAnimation (尝试用动画删除时钟 View ,因此它在主视图中不再可见)时,什么也没有发生。为什么?我真的不明白为什么。我已经尝试解决这个问题几个小时了,但我找不到任何方法让它发挥作用......

@implementation MainViewController {
ClockView *_clock;
ClockView *_countdown;
}

viewDidLoad:

-(void)viewDidLoad{
timerAddClock = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkNotificationExist) userInfo:nil repeats:NO];
}

检查通知是否存在:

 -(void)checkNotificationExist {
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];

if ([userDef boolForKey:@"NotificationExist"]) {
NSDate *notificationDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"NotificationDate"];
NSDate *now = [NSDate date];

NSTimeInterval interval = [notificationDate timeIntervalSinceDate:now];

if (interval > 0) {
if (_clock == nil) {
_countdown = [[ClockView alloc] initWithCountdownToTime:[NSDate dateWithTimeIntervalSinceNow:interval]];

[self.view addSubview:_countdown];

[UIView animateWithDuration:0.5f
animations:^{
[_countdown setFrame:CGRectMake((self.view.frame.size.width - _countdown.frame.size.width) / 2, (self.view.frame.size.height), _countdown.frame.size.width, _countdown.frame.size.height)];
}];
}
self.timeIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTime) userInfo:nil repeats:YES];
}
}

检查时间

-(void)checkTime {
NSLog(@"running");


[timerAddClock invalidate];
self.timerAddClock = nil;

NSDate *notificationDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"NotificationDate"];
NSDate *now = [NSDate date];

NSTimeInterval interval = [notificationDate timeIntervalSinceDate:now];

if (interval < 1) {
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setBool:NO forKey:@"NotificationExist"];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"NotificationDate"];

self.addAnimation = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeAnimation) userInfo:nil repeats:YES];
}

删除动画

-(void)removeAnimation {
NSLog(@"Removing animation...");

[UIView animateWithDuration:0.5f
animations:^{
[_countdown setFrame:CGRectMake((self.view.frame.size.width - _countdown.frame.size.width) / 2, (self.view.frame.size.height - 800), _countdown.frame.size.width, _countdown.frame.size.height)];

} completion:^(BOOL finished) {
[_countdown removeFromSuperView];
}];

最佳答案

首先,您应该在 - (void)viewDidLoad; 方法中调用 [super viewDidLoad]; 。另外,您可能已将 _countdown View 添加为某处的 subview ,这可能会导致问题?

您可能还想再次检查所有 NSTimer,并确保它们被正确调用。我觉得你的 addAnimation 计时器很可疑。

您可以尝试隐藏 View ,而不是将其从 super View 中删除。这肯定会起作用。

希望有帮助!

关于iphone - 在 View 中添加/删除 View 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424761/

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