DemoViewController 负责向用户展示教程。包含动画和计时器以在用户忽略时重复手势演示。是从 DataViewController 实例化的。为 nil-ed,但后来在其内部计时器上重生。我需要它完全消失,这样当用户返回第一页时就不会再次创建它。
数据视图控制器.h
#import "DemoViewController.h"
@property (strong,nonatomic) DemoViewController *demoController;
数据视图控制器.h
-(void) viewWillAppear:(BOOL)animated {
// demoPageNumber is 0
if ((self.demoController== nil) && ([_pageNumber isEqualToNumber:demoPageNumber])){
self.demoController = [[DemoViewController alloc] initWithView:self.view];
}
}
-(void) viewWillDisappear:(BOOL)animated{
[self.demoController free]; // invalidate timer, nil all internal objects
self.demoController=nil; // should free object
}
DemoViewController.m
-(void) free{
[animationRespawnTimer invalidate];
animationRespawnTimer=nil;
}
-(void) respawnDemoWithSelector:(SEL)selector{
NSLog(@"Timer fired %@", self);
[self resetTimer];
animationRespawnTimer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:selector
userInfo:nil
repeats:NO];
}
-(void) showScrollDemo{
NSLog(@"showScrollDemo fired");
[self stopPreviousAnimations];
scrollHandView.frame = CGRectMake(315.0, 700.0, 100, 100);
scrollHandView.hidden=NO;
scrollHandView.alpha=1;
[UIView animateWithDuration:3.0
delay: 0.0
options: (UIViewAnimationOptionCurveEaseOut |
UIViewAnimationOptionRepeat )
animations:^{
[UIView setAnimationRepeatCount:3];
scrollHandView.frame = CGRectMake(315.0, 300.0, 100, 100);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0 delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
scrollHandView.alpha=0;
}
completion:^(BOOL finished){
scrollHandView.hidden=YES;
[self respawnDemoWithSelector: @selector(showScrollDemo)];
}
];
}
];
}
当页面加载时,如果这是第一页,demoController 被实例化,并在清理后退出页面 nil-ed(自定义免费方法)。根据我的理解,这应该删除 demoController 对象及其所有内容,包括计时器。调试区域正好显示了这一点!直到在新页面上,demoController 计时器神秘地以先前的对象 ID 从无处重生。
17:59:14.041 viewWillAppear begin (self.demoController null)
18:00:05.346 viewWillAppear, <DemoViewController: 0x7580310> //demoController created
18:00:15.786 in the demoController method the "showScrollDemo" is fired
18:00:19.834 viewWillAppear end <DemoViewController: 0x7580310>
页面已加载,演示执行良好。现在我正在翻页。viewWillDisappear 事件被触发。
18:01:17.966 viewWillDisappear begin, send "free" message to demoController
18:01:17.966 "free" was performed from <DemoViewController: 0x7580310>
18:01:34.059 viewWillDisappear end (self.demoController null)
因此,“self.demoController”为空。然后 demoController 使用之前的 ID 重新生成自己
18:02:36.514 Timer fired <DemoViewController: 0x7580310>
为什么?定时器不能重生,它设置为repeats:NO。