1

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

在圈子被杀死之后,虽然我希望另一个圈子产生并做同样的事情(淡入和淡出到 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 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似乎无法正常工作

4

1 回答 1

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];
    }];
}
于 2012-11-20T20:07:41.757 回答