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

标签 ios objective-c uiviewanimation

我想在 KeyWindow( [[[UIApplication sharedApplication ].delegate window ] ) 中添加一个工作表,当用户点击头像时会有动画。

通过更改约束的常数,我希望工作表直接显示 downToUp ,而不是从原点(左端点和下端点)。
该问题有时会发生,有时会按预期运行。

我猜协调器布局很重要。

my Question Image

这是我的代码:创造和展示。

@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:工作表从场景底部开始。

enter image description here

最佳答案

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

- (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) {}];
    }
}

关于ios - NSLayoutConstraint,从原点更改常量动画,不是我设置的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47195521/

相关文章:

ios - 如何打开 Podfile 进行编辑

ios - Swift - 调用初始值设定项,缺少参数

ios - 动画颜色从上到下变化,反之亦然

ios - 为什么我在收到 UIKeyboardWillShowNotification 后更改其框架的 UIView 会自动设置动画?

ios - 如何为签名绘制平滑的 UIBezierPath?

ios - 即使不再显示,SwiftUI View 也会不断更新

ios - 找不到 CCRendererSharedTypes 文件

iOS 7 - 禁用 UITabBarItem 色调颜色

ios - Xcode 11.1 在iOS 13.1.2设备上运行测试,设置断点

ios - 圆形 UIView 上的 CGAffineTransformMakeScale 动画为正方形