performSelector:withObject:afterDelay方法无法正常工作?

时间:2012-11-20 18:37:56

标签: iphone objective-c ios methods selector

我有一个带有小uiimageview圈子的应用程序,可以在两个不同的位置进行淡入淡出。当用户触摸屏幕时,我希望圆圈立即淡出,我希望它能够停止淡入淡出到移动位置。所以基本上触摸屏幕会杀死圆圈。

虽然我希望另一个圆圈产生并做同样的事情(淡入和淡出到两个不同的位置)直到用户触摸屏幕,然后我希望该圆圈被杀死,另一个圆圈产生并且等...

这是我的简化代码:

- (void)spawnCircle {
    self.circle = [[UIImageView alloc]initWithFrame:self.rectCircle];//alocate it and give it its first frame
    self.circle.image=[UIImage imageNamed:@"circle.png"];//make its image the circle image
    [self.view addSubView:self.circle];
    [self performSelector:@selector(fadeCircleOut)withObject:self afterDelay:2];//the circle will fade out after 2 seconds
    self.isFadeCircleOutNecessary=YES;
}

- (void)fadeCircleOut {

    if (self.isFadeCircleOutNecessary){//because after the circle fades in this method is scheduled to occur after 2 seconds, well, if the user has touched the screen within that time frame we obviously don't want this method to be called because we already are fading it out
        [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{//the circle will fade out for a duration of .5 seconds
            self.circle.alpha=0;
        } completion:^(BOOL finished) {
            if (finished) {
                if (self.circle.frame.origin.x==self.rectCircle.origin.x) {//if its in the first location go to the second
                    self.circle.frame=self.rectCircle2;

                }else{//if its in the second location go to the first
                    self.circle.frame=self.rectCircle;
                }
                [self fadeCircleIn];//now were going to immediately fade it in its new location
            }
        }];
    }
}

- (void)fadeCircleIn {
    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{//fade it in with a duration of .5 seconds
        self.circle.alpha=1;
    } completion:^(BOOL finished) {
        if (finished) {
            [self performSelector:@selector(fadeCircleOut) withObject:self afterDelay:2];//after 2 seconds the object will fade out again
        }
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^(){ self.circle.alpha=0;} completion:^(BOOL completion){//begin from current state makes it stop any animations it is currently in the middle of

        [self spawnCircle];//now another circle will pop up
        self.isFadeCircleOutNecessary=NO;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self spawnCircle];
}

因此第一次圆圈产生效果很好但是当下一个圆圈产生时(在用户触摸屏幕并杀死第一个圆圈之后),淡出方法不会在2秒后完全发生,它会在发生时发生变化但它通常几乎会瞬间淡出。所以performSelector:withObject:afterDelay的延迟部分似乎无法正常工作

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,并得到了你所做的同样的结果。我不确定performSelector后面幕后发生了什么:withObject:afterDelay:,但是看起来就好了,即使你创建了另一个圈子,它仍然可以执行它的功能。

我通过删除afterDelay调用稍微更改了代码,但是在淡出方法中放入了2秒延迟。看看这是否符合您的要求:

-(void)spawnCircle{
    self.circle=[[UIImageView alloc]initWithFrame:self.rectCircle];//alocate it and give it its first frame
    self.circle.image=[UIImage imageNamed:@"circle.png"];//make its image the circle image
    [self.view addSubview:self.circle];
    [self fadeCircleOut];
}

- (void)fadeCircleOut {

    [UIView animateWithDuration:.5 delay:2 options:UIViewAnimationOptionCurveLinear animations:^{//the circle will fade out for a duration of .5 seconds
        self.circle.alpha=0;
    } completion:^(BOOL finished) {
        if (finished) {
            if (self.circle.frame.origin.x==self.rectCircle.origin.x) {//if its in the first location go to the second
                self.circle.frame=self.rectCircle2;

            }else{//if its in the second location go to the first
                self.circle.frame=self.rectCircle;
            }
            [self fadeCircleIn];//now were going to immediately fade it in its new location
        }
    }];
}

- (void)fadeCircleIn {
    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{//fade it in with a duration of .5 seconds
        self.circle.alpha=1;
    } completion:^(BOOL finished) {
        if (finished) {
            [self fadeCircleOut];
        }
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.circle.alpha=0;
    }
                     completion:^(BOOL completion){//begin from current state makes it stop any animations it is currently in the middle of
                         [self.circle removeFromSuperview];
                         self.circle = nil;
                         [self spawnCircle];
    }];
}
相关问题