3

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

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

viewDidLoad:

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

checkNotification存在:

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

3 回答 3

1

One possible explanation is that you might be adding more of one of that _countdown views. So if checkNotifications is called several times (there is a timer with an interval, so I assume that is possible) and the line where to add the _countdown view is run more than once, then if you only remove on of the views you won't see anything because the repeated view would be on top.

Just an idea.

于 2012-11-16T22:09:54.573 回答
1

首先,你应该调用[super viewDidLoad];你的- (void)viewDidLoad;方法。此外,您可能已将_countdown视图添加为某处的子视图,这可能会导致问题?

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

您可以尝试仅隐藏视图而不是将其从超级视图中删除。那肯定行得通。

希望有帮助!

于 2012-11-16T22:13:09.537 回答
0

根据您最后的评论,当您调用 removeFromSuperview 时,似乎有一个动画正在进行中。因此,视图不会被删除。

我认为问题出在您的 checkNotificationExists 方法中:您创建了一个动画,但在完成块之外启动了计时器。尝试将其移动到完成块内

于 2012-11-16T22:41:16.757 回答