ios - CABasicAnimation 始终不工作

标签 ios objective-c swift cabasicanimation catransaction

我有以下代码,应该给出 View “摇动”的效果,然后通过向上滑出屏幕来消除它。

有时它呈现得很完美,但大多数时候摇动动画不会显示, View 只会向上滑动。我不知道问题出在哪里:

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

最佳答案

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

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

然后,摇动部分将首先发生,然后才开始按照您想要的方式向上移动。

关于ios - CABasicAnimation 始终不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979729/

相关文章:

iphone - AUIOClient_StartIO 尝试在后台播放时失败

objective-c - CLGeocoder 反向地理编码位置。第一次在 [placemarks count = 0]?

ios - 属性数组不添加对象,临时数组会。

iOS:如果触摸在特定区域内,则发送触摸事件以查看后面

ios - 对 'iPhoneSimulator' 使用 sys root,但在 NPM 安装中针对 'MacOSX' 警告

ios - EXIF 的 PHContentEditingInputRequestOptions block 主线程

ios - UITableview/UIView 委托(delegate)混淆

iphone - 在 NSUserDefaults 中设置值时崩溃

ios - XLPagerTabStrip 选择首先显示的 View Controller

ios - 如何在SwiftUI分段选择器中更改选定的分段颜色