iOS:如何为新的自动布局约束(高度)设置动画 [英] iOS: How does one animate to new autolayout constraint (height)

查看:33
本文介绍了iOS:如何为新的自动布局约束(高度)设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过自动布局 约束.我有一个正在开发的小型新应用程序,并注意到 NIB 的视图默认为自动布局.所以,我想我会借此机会使用它并尝试弄清楚 Apple 将如何处理它.

I've never worked with autolayout constraints before. I have a small new app I'm working on and noticed that the NIB's views are defaulting to autolayout. So, I figured I'd take the opportunity to work with it and try to figure out where Apple is going with this.

第一个挑战:

我需要调整 MKMapView 的大小,我想将其动画化到新位置.如果我按照我习惯的方式这样做:

I need to resize an MKMapView and I'd like to animate it to the new position. If I do this the way I'm used to:

[UIView animateWithDuration:1.2f
     animations:^{
         CGRect theFrame = worldView.frame;
         CGRect newFrame = CGRectMake(theFrame.origin.x, theFrame.origin.y, theFrame.size.width, theFrame.size.height - 170);
         worldView.frame = newFrame;
}];

...然后,每当同级视图更新时,MKMapView 将快速"回到其原始高度(在我的情况下,UISegmentedControl 的标题正在更新 [myUISegmentedControl setTitle:newTitle forSegmentAtIndex:0]).

...then the MKMapView will 'snap' back to its original height whenever a sibling view gets updated (in my case a UISegmentedControl's title is being updated [myUISegmentedControl setTitle:newTitle forSegmentAtIndex:0]).

所以,我认为我想做的是将 MKMapView 的约束从等于父视图的高度更改为相对于它是的 UISegmentedControl 的顶部 覆盖:V:[MKMapView]-(16)-[UISegmentedControl]

So, what I think I want to do is change the constraints of the MKMapView from being equal to the parent view's hight to being relative to the top of the UISegmentedControl that it was covering: V:[MKMapView]-(16)-[UISegmentedControl]

我想要的是缩短 MKMapView 的高度,以便显示地图视图下方的一些控件.为此,我认为我需要将约束从固定的全尺寸视图更改为底部约束到 UISegmentedControl 顶部的约束......我希望它作为视图动画缩小到新尺寸.

What I want is for the MKMapView height to shorten so that some controls beneath the map view are revealed. To do so I think I need to change the constraint from a fixed full size view to one where the bottom is constrained to the top of a UISegmentedControl...and I'd like it to animate as view shrinks to new size.

如何处理这个问题?

编辑 - 这个动画不是动画,尽管视图的底部确实会立即向上移动 170 度:

Edit - this animation is not animating though the bottom of the view does move up 170 instantly:

    [UIView animateWithDuration:1.2f
         animations:^{
             self.nibMapViewConstraint.constant = -170;

    }];

并且 nibMapViewConstraint 在 IB 中连接到底部垂直空间约束.

and the nibMapViewConstraint is wired up in IB to the bottom Vertical Space constraint.

推荐答案

更新约束后:

[UIView animateWithDuration:0.5 animations:^{[self.view layoutIfNeeded];}];

self.view 替换为对包含视图的引用.

Replace self.view with a reference to the containing view.

这篇关于iOS:如何为新的自动布局约束(高度)设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆