NSTimers和NSRunLoops的行为异常 [英] Erratic behavior with NSTimers and NSRunLoops

查看:29
本文介绍了NSTimers和NSRunLoops的行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CGAffineTransformMakeTranslation()从右向左制作一个 UIImageView (手指的图像)的动画.此动画将重复进行,直到用户执行滑动操作,然后在教程中将用户向前移动.所有这一切都已经按照如下方式进行顺畅地工作:

I am attempting to have a UIImageView (a picture of a finger) animate from right to left, using CGAffineTransformMakeTranslation(). This animation will repeat until the user performs a swipe, moving the user along in a tutorial. All of this is already working swimmingly as follows:

[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];


            }];

        }];

以及选择器:

-(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;

    }];
}];
}

手指动起来动起来很漂亮.

The finger animates and translates beautifully.

当我想用带有 different 计时器的 different 手指来执行此操作时,就会出现问题.我有相同的 exact 代码,但是它是不同的手指和计时器的选择器.

The issue comes when I want to do this with a different finger with a different timer. I have the same exact code, however it is a different finger and different selector for the timer.

发生的事情是计时器的选择器将转换UIImageView,并且(更可怕的是)当我调用使该方法无效的方法时,该计时器将不会无效.调试后,我看到第二个计时器正在调用第二个选择器,但行为不佳(例如,未进行翻译,并且第二根手指的褪色速度太快).

What happens is that the timer's selector will not translate the UIImageView, and (more scary) the timer will not invalidate when I call the method to invalidate. Upon debugging, I see that the the 2nd timer is calling the 2nd selector, but just not behaving (e.g. not translating, and fading in the 2nd finger too rapidly).

我所假设的是,我第一次调用NSRunLoop时需要以某种方式关闭它吗?这是第一次使用NSRunLoop,因此对我的无知表示歉意.任何帮助都非常有用.

What I am assuming is that I need to somehow turn off the NSRunLoop when I first call it? This is the first time working with NSRunLoop so I apologize for my ignorance. Any help is very much appricated.

推荐答案

好吧,您当然有一个块保留周期.您需要使用__block或__weak说明符.参见块和变量.您的问题可能与内存问题有关.

Well, you certainly have a block retain cycle going on. You need to use either the __block or __weak specifier. See Blocks and Variables. Your problem may be related to a memory issue.

请确保您在完成首次计时后将其无效.

Make sure you invalidate the first timer when it is done.

为了安全起见,您可能还想在尝试转换UIImageView之前重置它.您可以这样操作:

For safety, you may also want to reset the transform on your UIImageView before attempting to transform it. You can do that like so:

finger.transform = CGAffineTransformIdentity;

这篇关于NSTimers和NSRunLoops的行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆