CABasicAnimation不能一直工作

时间:2015-08-13 04:50:20

标签: ios objective-c cabasicanimation

我有以下代码可以提供视图效果"摇动"然后从屏幕上滑下来解雇。有时它呈现完美,但大多数时候摇动动画不会显示,视图只会向上滑动。我无法弄清问题是什么:

CABasicAnimation *animation =
        [CABasicAnimation animationWithKeyPath:@"position"];
        [animation setDuration:0.05];
        [animation setRepeatCount:8];
        [animation setAutoreverses:YES];
        [animation setFromValue:[NSValue valueWithCGPoint:
                                 CGPointMake([[self editView] center].x - 20.0f, [[self editView] center].y)]];
        [animation setToValue:[NSValue valueWithCGPoint:
                               CGPointMake([[self editView] center].x + 20.0f, [[self editView] center].y)]];
        [[[self editView] layer] addAnimation:animation forKey:@"position"];

        [CATransaction begin]; {
            [CATransaction setCompletionBlock:^{


                [UIView animateWithDuration:0.4f delay:0.7 options:UIViewAnimationOptionTransitionNone animations:^{

                    _editView.center = CGPointMake(self.navigationController.view.bounds.size.width / 2, 0 - 380);
                    [_textView resignFirstResponder];

                    [_tint setAlpha:0];


                } completion:^(BOOL finished) {

                    [_editView removeFromSuperview];
                    [_tint removeFromSuperview];
                }];

            }];

        } [CATransaction commit];

1 个答案:

答案 0 :(得分:1)

您需要做的唯一更改是重新放置:

[[[self editView] layer] addAnimation:animation forKey:@"position"];

[CATransaction commit];之上。

因此,它变成了:

[CATransaction begin];
{
    [CATransaction setCompletionBlock:^
    {
        [UIView animateWithDuration:0.4f delay:0.7f options:UIViewAnimationOptionTransitionNone animations:^
        {
            _editView.center = CGPointMake(self.navigationController.view.bounds.size.width / 2.0f, -380.0f);
           [_textView resignFirstResponder];
           [_tint setAlpha:0];
        }
        completion:^(BOOL finished)
        {
            [_editView removeFromSuperview];
            [_tint removeFromSuperview];
        }];
    }];
}
[[[self editView] layer] addAnimation:animation forKey:@"position"];
[CATransaction commit];

然后,摇动部分将首先发生,然后开始按照您的意愿向上动画。