在UIView动画块中调用layoutIfNeeded会导致子视图提前移动

时间:2014-03-24 05:12:46

标签: ios objective-c animation uiview cgaffinetransformscale

我正在尝试创建自定义标注气泡。 我有一个动画块设置气泡UIView的比例变换:

self.view.transform = CGAffineTransformMakeScale(0, 0); // (1)
self.view.transform = CGAffineTransformIdentity; // (2)

我要么像(1)那样用缩放(0,0)开始泡泡视图,要么在动画块内动画像(2)中的身份,或者从(2)变为(1)。 / p>

如果我的动画块中没有[self.view layoutIfNeeded];,则从(1)到(2)的工作正常,如下所示:

enter image description here

但是当没有[self.view layoutIfNeeded];从(2)回到(1)时,子视图会在动画结束前跳到左侧:

enter image description here

现在,如果我使用气泡视图添加[self.view layoutIfNeeded];子视图动画,但有一些延迟:

从(1)到(2):

enter image description here

或者从(2)到(1):

enter image description here

我已经尝试用How do I adjust the anchor point of a CALayer, when Auto Layout is being used?中的中心约束替换所有顶部和前导子视图的约束,并且还尝试了图层变换解决方案(但是这个解决了布局约束,说它不能满足所有约束)

关于如何解决我的动画问题的任何想法?

提前致谢。

更新 我正在用我调用的实际方法更新问题

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotationView

显示和关闭我的自定义标注气泡(暂时忘记扩展状态)。

- (void)setCalloutState:(CalloutState)calloutState
               animated:(BOOL)animated
         annotationView:(MKAnnotationView *)annotationView
             completion:(void (^)(BOOL finished))completion
{
    if (self.view.superview == annotationView || !annotationView) {

    } else {
//        [self.view.layer removeAllAnimations];
        [self.view removeFromSuperview];

        self.view.transform = CGAffineTransformIdentity;
        self.view.bounds = CGRectMake(0, 0, normalViewWidth, normalViewHeight);
        self.view.transform = CGAffineTransformMakeScale(0, 0);
        self.view.center = CGPointMake(annotationView.bounds.size.width / 2, 0);

        [annotationView addSubview:self.view];
    }

    void (^animationBlock)(void) = ^{
        self.view.transform = CGAffineTransformIdentity;

        switch (calloutState) {
            case CalloutStateHidden:
                self.view.bounds = CGRectMake(0, 0, normalViewWidth, normalViewHeight);
                self.view.transform = CGAffineTransformMakeScale(0, 0);
                break;
            case CalloutStateNormal:
                self.view.bounds = CGRectMake(0, 0, normalViewWidth, normalViewHeight);
                break;
            case CalloutStateExpanded:
                self.view.bounds = CGRectMake(0, 0, expandedViewWidth, expandedViewHeight);
                break;
            default:
                break;
        }

        self.view.center = CGPointMake(annotationView.bounds.size.width / 2, 0);

        [self.view layoutIfNeeded];
    };

    if (animated) {
        // TODO: figure out why the first animateWithDuration is needed in this nested thing
        [UIView animateWithDuration:0
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:NULL
                         completion:^(BOOL finished) {

                             [UIView animateWithDuration:1.3
                                                   delay:0
                                                 options:UIViewAnimationOptionBeginFromCurrentState
                                              animations:animationBlock
                                              completion:^(BOOL finished) {
                                                  if (finished) {
                                                      self.calloutState = calloutState;

                                                      // TODO: figure out how to end UIView animation instantly so we don't need the second condition at the if
                                                      // having a concurrency problem here
                                                      if (calloutState == CalloutStateHidden && self.view.superview == annotationView) {
                                                          [self.view removeFromSuperview];
                                                      }

                                                      self.editingEnabled = calloutState == CalloutStateExpanded;
                                                  }

                                                  if (completion) {
                                                      completion(finished);
                                                  }
                                              }];

                         }];
        // ---------------------------------------------------------------------------------
    } else {
        animationBlock();

        self.calloutState = calloutState;
        if (calloutState == CalloutStateHidden) {
            [self.view removeFromSuperview];
        }

        self.editingEnabled = calloutState == CalloutStateExpanded;

        if (completion) {
            completion(YES);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助。根据Apple文档-layoutIfNeeded提前布局

- (void)setNeedsLayout;
- (void)layoutIfNeeded;

所以,你可以试试setNeedsLayout。这应该满足你提到的第一种情况。