如何使用具有自动布局约束的动画移动视图已开启 iOS

How To shift a view with animation with auto layout constraints are on iOS

我正在执行一项任务,我需要将视图移动 35 像素以显示该视图下方的视图。

或者 你可以说我需要在顶部插入一个类似通知的栏,并在运行时将现有视图移动 35 像素。

问题在于为了支持所有屏幕而启用了自动布局。换档没有按照我们的意愿发生......这里有什么建议吗? 顶视图有一个约束,它与顶视图的距离为零,需要在运行时更改,当我们显示下面的视图时。

转移前的画面如下:

移动并显示图像后:

我也给出了侧边栏视图。 NotifView :我们需要在运行时显示的黄色视图 twoView : 我们需要切换到在顶部显示 notifview。

-(void)shiftTwoView:(NSString*)summary{

CGFloat shiftHeight = 35.0f;

CGRect twoRect = twoView.frame;
    twoRect.origin.y += shiftHeight;
    twoRect.size.height -= shiftHeight;


[UIView animateWithDuration:3.5
                 animations:^{
                     twoView.frame = twoRect;
                 }];

}

当您使用自动布局时,您不需要那样设置框架。您可以通过编程方式操作约束值来执行此操作。由于您还没有明确说明您在此处应用了哪些约束,我将解释您需要这样做的所有内容。

notifyview: - leading = 0, trailing = 0, top = 0, height = 0;  
twoView: - leading = 0, trailing = 0, top(to notifview) = 0; bottom = 0;

现在为 notifyview 的高度约束创建一个 IBOutlet,假设 notifyViewHeightConstraint

现在要显示notifyView 的内容,您需要做的就是增加它的高度。默认情况下,此高度是情节提要中的 0。您可以像 notifyViewHeightConstraint.constant = 40; 那样更改它。完成后需要再次将其消失,您可以将 constant 值设置回 0;

-(void)shiftTwoView:(NSString*)summary  
{  
     notifyViewHeightConstraint.constant = 40;   
    [UIView animateWithDuration:2.0 animations:^{  
            [self.view layoutIfNeeded];  
    }]; 
}