OGeek|极客世界-中国程序员成长平台

标题: iOS 布局动画在 GMSMapView 拖动时忽略持续时间 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:21
标题: iOS 布局动画在 GMSMapView 拖动时忽略持续时间

您可以在下面比较两个 gif 时看到问题。 “收缩”和“拉伸(stretch)”动画都基于约束操作,并且 [self layoutIfNedeed] 放置在 -animateWithDuration block 内。如您所见,本地图不移动时,它会按预期工作。但是当您开始拖动 map 时,“缩小”动画会立即发生。如果您仔细观察,您可能会注意到cornerRadius 动画仍然有效。这告诉我它可能与 map 有关(里面的 ScrollView ?不知道谷歌地图是如何工作的)调用它自己的布局 block 会干扰我的动画。也可能是因为cornerRadius 动画是使用CABasicAnimation 完成的,而不是UIView 动画。

Animated InfoView 不是 mapView 的 subview 。不确定当 mapView 调用内部布局方法时它是否会影响我的 infoView 的布局。

我已经尝试了所有组合的选项 UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionOverrideInheritedDuration | UIViewAnimationOptionOverrideInheritedOptions 只是看看它是否有帮助。它没有。

我想我可以直接克服这个操纵框架而不是操纵约束,它对用户来说看起来是一样的,但我觉得使用约束要优雅得多。我尽量避免操纵框架并使用自动布局。

任何想法是什么导致了这种情况以及如何使用自动布局动画来解决这个问题?

更新

我尝试过直接为帧设置动画 - 结果是一样的。所以它与自动布局无关。似乎当前 View 动画由于某种原因停止了。

如果我用 CABasicAnimation 替换我的 UIView 动画 block ,它就像一个魅力。

我被要求发布一些动画代码:

- (void)shrink {

    if (!self.isStretched) return;

    self.stretched = NO;

    [self hideImageAndLabels];

    [self.indicatorView startAnimating];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath"cornerRadius"];
    animation.duration = duration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fromValue = @0;
    animation.toValue = @(shrinkedSize / 2.0);

    [self.layer addAnimation:animation forKey"cornerRadiusAnimationRounded"];
    self.layer.cornerRadius = shrinkedSize / 2.0;

    self.heightConstraint.constant = shrinkedSize;
    self.widthConstraint.constant = shrinkedSize;
    self.positionYConstraint.constant = 0;
    self.triangleHeightConstraint.constant = 0;
    self.trianglePositionYConstraint.constant = 14;

    [UIView animateWithDuration:duration * 2 delay:0 usingSpringWithDamping:0.85 initialSpringVelocity:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
            //
    }];


}

enter image description here enter image description here



Best Answer-推荐答案


我在 Google (https://code.google.com/p/gmaps-api-issues/issues/detail?id=10349) 上打开了一个关于此问题的错误,这是他们的回复:

Our understanding is that this is happening because the mapView:willMove: call is being made while already inside a CoreAnimation transaction. Our suggested work around is to wrap the UIView.animateWithDuration call within a dispatch_async back to the main thread. Something along the lines of:

dispatch_async(dispatch_get_main_queue(), ^{ // animation block here. });

Does that help?

将 UIView.animateWithDuration block 放在 dispatch_async block 中对我有用。

关于iOS 布局动画在 GMSMapView 拖动时忽略持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399099/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4