与NSTimers和NSRunLoops不稳定的行为

我正在尝试使用CGAffineTransformMakeTranslation()从右到左有一个UIImageView (一个手指的图片)animation。 这个animation将重复,直到用户执行一个滑动,并在教程中移动用户。 所有这些已经在游泳中起作用了,如下所示:

 [UIView animateWithDuration:1.0 animations:^ { self.finger.alpha = 1.0; }completion:^(BOOL finished) { CGAffineTransform originalTransform = self.finger.transform; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ { self.finger.transform = CGAffineTransformMakeTranslation(-200, 0); self.finger.alpha = 0.0; }completion:^(BOOL finished) { self.finger.transform = originalTransform; NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatSwipeAnimation1) userInfo:nil repeats:YES]; self.swipeTimerForFinger1 = timer; NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:self.swipeTimerForFinger1 forMode:NSDefaultRunLoopMode]; [self.swipeTimerForFinger1 fire]; }]; }]; 

而select器:

 -(void)repeatSwipeAnimation1 { [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^ { self.finger.alpha = 1.0; }completion:^(BOOL finished) { CGAffineTransform originalTransform = self.finger.transform; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^ { self.finger.transform = CGAffineTransformMakeTranslation(-200, 0); self.finger.alpha = 0.0; }completion:^(BOOL finished) { self.finger.transform = originalTransform; }]; }]; } 

手指animation翻译精美。

当我想用不同的计时器用不同的手指来做这个事情的时候会出现问题。 我有相同的确切代码,但它是一个不同的手指和不同的select器的计时器。

会发生什么是计时器的select器不会翻译UIImageView,(更可怕)的计时器不会失效时,我调用的方法无效。 在debugging的时候,我看到第二个定时器正在调用第二个select器,但是没有performance(例如,不能快速地翻译和褪色第二个手指)。

我所假设的是,当我第一次打电话时,我需要以某种方式closuresNSRunLoop? 这是第一次与NSRunLoop合作,所以我为我的无知道歉。 任何帮助非常appricated。

那么,你肯定有一个块保留周期继续。 您需要使用__block或__weak说明符。 请参阅块和variables 。 您的问题可能与内存问题有关。

确保在第一个定时器完成后使其无效。

为了安全起见,您可能还想在尝试转换之前重置UIImageView上的转换。 你可以这样做:

 finger.transform = CGAffineTransformIdentity;