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

checkNotificationExist:

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

checkTime

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

removeAnimation

-(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

考えられる理由の1つは、その_countdownビューの1つをさらに追加している可能性があるということです。したがって、checkNotificationsが数回呼び出され(間隔のあるタイマーがあるので、それは可能だと思います)、_ countdownビューを追加する行が複数回実行される場合、ビューの1つだけを削除すると'繰り返されるビューが上になるため、何も表示されません。

ただのアイデア。

于 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 に答える