首页 文章

我的animatewithduration,完成块只执行一次

提问于
浏览
4

我使用下面的代码块向下滑动UIView,完成后再旋转另一个UIView . 动画的第二部分,完成块仅执行一次,这意味着第一个动画未完成,否则它将到达完成块 . 在iPhone模拟器上,它看起来好像第一个动画完成了......任何人都可以帮我解决这个问题吗?我的NSLog说:

完成第1次开始第2完成第1完成第1完成第1 . . .

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

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

    BTW:如果你问我:)块代码需要一些严肃的重构

  • 2

    你正在混合和匹配范式,我相信这会引起你所看到的问题 . 您正在创建一个动画块,但在该块内部,您正在创建一个新的动画例程,其中包含用于运行UIView动画的“旧”范例 . Apple正在引领人们远离旧范式,我也鼓励你们只使用块 .

    这就是为什么完成块只运行一次,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;
                                                                }];
                                 }];
    }
    

相关问题