NSLayoutConstraint,从原点改变常量动画,不是我设置的

NSLayoutConstraint, change constant Animation form the origin, Not what I have set

我想添加一个 sheet 到 KeyWindow( [[[UIApplication sharedApplication ].delegate window ] ),当用户点击头像时带有动画。

我希望 sheet 通过更改约束常量直接显示为 downToUp,而不是从原点(左侧和底部端点)。 该问题有时会发生,有时会按预期运行。

我想协调器布局很重要。

这是我的代码:创建并显示。

@implementation ZBPhotoSheetView

- (void)awakeFromNib
{
    [super awakeFromNib ];
    self.backgroundColor = [[UIColor blackColor ] colorWithAlphaComponent: 0.3 ];
    //self.vesselViewBottomConstraint.constant = -150; // add it or not ,don't like it matters.
}



+ (ZBPhotoSheetView *)createZhePhotoSheetView
{
    ZBPhotoSheetView * zheSheetView = [[[NSBundle mainBundle ] loadNibNamed:@"ZBPhotoSheetView" owner: nil options: nil ] firstObject ];
    return zheSheetView;
}


- (void)showZhePhotoSheetView{
    if (!_sheetIsShowing){
        [[[UIApplication sharedApplication ].delegate window ] addSubview: self ];
        self.frame = [UIScreen mainScreen ].bounds;
        _sheetIsShowing = YES;

        [UIView animateWithDuration: 0.3f delay:0.f options: UIViewAnimationOptionCurveEaseIn animations:^{
            self.vesselViewBottomConstraint.constant = 0;
           // [self setNeedsUpdateConstraints ]; // add it or not ,don't like it matters.
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {}];
    }
}

这是我的 Xib: sheet 从场景底部开始。

您需要在更改约束常量之前调用 layoutIfNeeded 一次。您刚刚在 window 中添加了视图,因此初始自动布局和您的约束在动画中一起发生变化。试试这个。

- (void)showZhePhotoSheetView{
    if (!_sheetIsShowing){
        [[[UIApplication sharedApplication ].delegate window ] addSubview: self ];
        self.frame = [UIScreen mainScreen ].bounds;
        _sheetIsShowing = YES;
        [self layoutIfNeeded];

        [UIView animateWithDuration: 0.3f delay:0.f options: UIViewAnimationOptionCurveEaseIn animations:^{
            self.vesselViewBottomConstraint.constant = 0;
           // [self setNeedsUpdateConstraints ]; // add it or not ,don't like it matters.
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {}];
    }
}