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

标签 iphone ios animation view ios6

我正在尝试向下面的 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/

相关文章:

iphone - AdMob 崩溃并显示 [GADObjectPrivate changeState :]: unrecognized selector

iphone - 更改 NSDate 对象的年月日

iphone - 如何访问和修改 UINavigationBar

ios - 运行时与 MPAndroidChart 的用户交互

javascript - 如何使用 javascript 停止 gif 动画以防止内存和 CPU 消耗过高

iOS 键盘出现所有动画故障

css - 使用 SX 属性创建动画 - MUI v5

ios - 在 iPhone 中以编程方式在沙箱外执行应用程序

php - 通过 Twillion 将联系人分享到 iPhone

iphone - 无法在 UIButton 中使用自定义字体