【问题标题】:How To shift a view with animation with auto layout constraints are on iOS如何在 iOS 上使用具有自动布局约束的动画移动视图
【发布时间】:2015-12-04 06:47:56
【问题描述】:

我正在执行一项任务,我需要将视图移动 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;
                 }];

}

【问题讨论】:

  • 在运行时使用 NSConstraint 设置您的自定义约束。设置时不要忘记将 setTranslatesAutoresizingMaskIntoConstraints 设置为 false。
  • 在这种情况下,我还应该从顶部删除我对它的约束?

标签: ios objective-c storyboard autolayout uiviewanimation


【解决方案1】:

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

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];  
    }]; 
}

【讨论】:

  • 您的建议不仅对我有所帮助,而且还让我学到了很多东西……很好的解决方案,谢谢……
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多