0

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。

4

1 回答 1

1

我假设它是调用respawnDemoWithSelector并创建一个新计时器的动画的完成块。

根据这个答案:https://stackoverflow.com/a/9676508/1187415,您可以停止所有正在运行的动画

[self.view.layer removeAllAnimations];

done或者,您可以向 DemoViewController添加一个布尔属性,该属性YESfree方法中设置为,并在动画的完成块中进行检查:

if (!self.done)
    [self respawnDemoWithSelector: @selector(showScrollDemo)];

更新:动画块捕获对 的强引用self,从而防止对象被释放。“保留周期”问题的标准解决方案(假设您使用 ARC)是使用对 self 的弱引用。看起来像这样:

__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:3.0
                      delay: 0.0
                    options: (UIViewAnimationOptionCurveEaseOut |
                              UIViewAnimationOptionRepeat )
                 animations:^{

                     [UIView setAnimationRepeatCount:3];
                     weakSelf.scrollHandView.frame = CGRectMake(315.0, 300.0, 100, 100);

                 }
                 completion:^(BOOL finished){

                     [UIView animateWithDuration:1.0 delay:0
                                         options:UIViewAnimationOptionCurveEaseOut
                                      animations:^{
                                          weakSelf.scrollHandView.alpha=0;

                                      }
                                      completion:^(BOOL finished){
                                          weakSelf.scrollHandView.hidden=YES;
                                          [weakSelf respawnDemoWithSelector: @selector(showScrollDemo)];

                                      }
                      ];

                 }
 ];

weakSelf不持有对 DemoViewController 的强引用,如果它指向的对象被释放,则自动设置nil 。在这种情况下,发送到weakSelf块内部的所有消息都什么都不做。

于 2013-06-30T16:50:28.620 回答