我正在尝试在下面的视图中添加/删除一个视图(_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];
}
检查通知是否存在:
-(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];
}];