【问题标题】:My animatewithduration, completion block is performed only once我的 animatewithduration,完成块只执行一次
【发布时间】:2012-03-30 20:03:57
【问题描述】:

我使用以下代码块向下滑动 UIView,并在完成后旋转另一个 UIView。 动画的第二部分,完成块只执行一次,这意味着第一个动画没有完成,否则它将到达完成块。 在 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;
         }];
}];

【问题讨论】:

  • 丹,这两个答案对您有帮助吗?你找到他们工作了吗?如果是这样,如果您“接受”最好的一个或根据您的发现制作 cmet,这对每个人都有帮助。干杯

标签: ios objective-c


【解决方案1】:

为什么要在 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 动画。 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;
                                                                }];
                                 }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 2021-10-16
      相关资源
      最近更新 更多