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

时间:2012-03-30 20:03:58

标签: ios objective-c

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

  

完成第一次

     

开始第二次

     

完成第一次

     

完成第一次

     

完成第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 个答案:

答案 0 :(得分: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:如果你问我,块代码需要一些严肃的重构:)

答案 1 :(得分:2)

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

这就是为什么完成块只运行一次,UIView animateWith块代码只运行一次。但是,您的内部动画代码会多次运行。

取出:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.7];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:NO];

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

  • animateWithDuration:延迟:选择:动画:完成:

使延迟= 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;
                                                            }];
                             }];
}