我的animateWithDuration方法中的completion block只执行了一次

4
我使用以下代码块来使一个UIView向下滑动,完成后旋转另一个UIView。 动画的第二部分——完成块(completion block)只会执行一次,这意味着第一部分动画没有完成,否则它将到达完成块。 在iPhone模拟器中,看起来第一个动画已经完成了... 有人能帮我解决这个问题吗? 我的NSLog输出如下:

完成第一个

开始第二个

完成第一个

完成第一个

完成第一个
.
.
.

- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 animations:^{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationRepeatAutoreverses:NO];

    CGPoint pos = movingtTable.center;
    float moveDistance = 220.0;
    if(!isViewVisible){
        //expose the view
        pos.y = pos.y+moveDistance;
        //disable selection for xy table
        xTable.userInteractionEnabled = NO;
        yTable.userInteractionEnabled = NO;
        //angle = M_PI;
    }
    else
    {
        pos.y = pos.y-moveDistance;
        xTable.userInteractionEnabled = YES;
        yTable.userInteractionEnabled = YES;
        //angle = -M_PI;
    }
    isViewVisible = !isViewVisible;
    movingtTable.center = pos;      
    NSLog(@"finished 1st");


}completion:^(BOOL finished){
    NSLog(@"started 2nd");
        [UIView animateWithDuration:0.4 animations:^{
            //[UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.4];
            //[UIView setAnimationRepeatCount:1];
            //[UIView setAnimationRepeatAutoreverses:NO];
            arrows.transform = CGAffineTransformMakeRotation(angle); 
         }completion:^(BOOL finished){
             angle = -angle;
         }];
}];

丹,这两个答案对你有帮助吗?你觉得它们有效吗?如果有的话,如果你'接受'最好的答案或对你的发现进行评论,那对每个人都有帮助。干杯! - MobileVet
2个回答

2

为什么你要在animateWithDuration代码块中尝试初始化另一个UIView动画?请更新你的代码如下,并确保不同时执行单个视图的多个动画。

- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 animations:^{
    CGPoint pos = movingtTable.center;
    float moveDistance = 220.0;
    if(!isViewVisible){
        //expose the view
        pos.y = pos.y+moveDistance;
        //disable selection for xy table
        xTable.userInteractionEnabled = NO;
        yTable.userInteractionEnabled = NO;
        //angle = M_PI;
    }
    else
    {
        pos.y = pos.y-moveDistance;
        xTable.userInteractionEnabled = YES;
        yTable.userInteractionEnabled = YES;
        //angle = -M_PI;
    }
    isViewVisible = !isViewVisible;
    movingtTable.center = pos;      
    NSLog(@"finished 1st");
}
completion:^(BOOL finished){
    NSLog(@"started 2nd");
        [UIView animateWithDuration:0.4 animations:^{
            arrows.transform = CGAffineTransformMakeRotation(angle); 
         }completion:^(BOOL finished){
             angle = -angle;
         }];
}];

顺便说一下:如果你问我,块代码需要进行一些严肃的重构 :)


2
您正在混合使用范式,我认为这正是导致您看到的问题。您正在创建一个动画块,但在该块内部,您正在使用“旧”的范例创建新的动画例程以运行UIView动画。苹果公司正在引导人们远离旧的范例,我鼓励您仅使用块。
这就是为什么完成块只运行一次,UIView animateWith块代码只运行一次。但是,您的内部动画代码会多次运行。
删除:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.7];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:NO];

如果您希望动画块运行多次,请使用完整的方法:

  • animateWithDuration:delay:options:animations:completion:

将延迟设置为0,并将选项设置为UIViewAnimationOptionRepeat,或者您需要完成块所需的循环次数。

以下是我的建议,假设您希望它重复:

- (IBAction) move 
{ 
[UIView animateWithDuration:0.7 
                      delay:0 
                    options:UIViewAnimationOptionRepeat 
                 animations:^{
                               CGPoint pos = movingtTable.center;
                               float moveDistance = 220.0;

                               if(!isViewVisible) {
                                 //expose the view
                                 pos.y = pos.y+moveDistance;
                                 //disable selection for xy table
                                 xTable.userInteractionEnabled = NO;
                                 yTable.userInteractionEnabled = NO;
                                 //angle = M_PI;
                               }
                               else {
                                 pos.y = pos.y-moveDistance;
                                 xTable.userInteractionEnabled = YES;
                                 yTable.userInteractionEnabled = YES;
                                 //angle = -M_PI;
                               }
                               isViewVisible = !isViewVisible;
                               movingtTable.center = pos;      
                               NSLog(@"finished 1st");
                             }
                completion:^(BOOL finished){
                               NSLog(@"started 2nd");
                               [UIView animateWithDuration:0.4 
                                                animations:^{
                                                               arrows.transform = CGAffineTransformMakeRotation(angle); 
                                                            }
                                                completion:^(BOOL finished){
                                                               angle = -angle;
                                                            }];
                             }];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接