126

これまで自動レイアウトの制約を扱ったことはありません。私が取り組んでいる小さな新しいアプリがあり、NIB のビューがデフォルトで自動レイアウトになっていることに気付きました。それで、私はこの機会を利用し、Apple がこれでどこに向かっているのかを理解しようと考えました。

最初の課題:

MKMapView のサイズを変更する必要があり、新しい位置にアニメーション化したいと考えています。私が慣れている方法でこれを行う場合:

[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]ます)。

だから、私がやりたいと思うのは、MKMapViewの制約を、親ビューの高さに等しいものから、それカバーしていたUISegmentedControlの上部に相対的なものに変更することです:V:[MKMapView]-(16)-[UISegmentedControl]

私が望むのは、MKMapView の高さを短くして、マップ ビューの下にあるいくつかのコントロールが表示されるようにすることです。そのためには、制約を固定のフル サイズ ビューから、下部が UISegmentedControl の上部に制限されるビューに変更する必要があると思います...ビューが新しいサイズに縮小するときにアニメーション化したいと思います。

これについてどうやって行くのですか?

編集 -このアニメーションはアニメーション化されていませんが、ビューの下部はすぐに 170 移動します:

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

    }];

IBでnibMapViewConstraint下部の垂直スペース制約に接続されています。

4

6 に答える 6

182

制約を更新した後:

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

self.view含まれているビューへの参照に置き換えます。

于 2013-03-13T04:30:50.197 に答える
3

この小さなデモを利用できるようにしました。非常に単純な例で、自動レイアウトの制約を変更してアニメーション化する方法を示します。DemoViewController.mを見てください。

于 2014-02-12T14:16:44.017 に答える
1

Most people use autolayout to layout items on their views and modify the layout constrains to create animations.

An easy way to do this without a lot of code is creating the UIView you want to animate in Storyboard and then creating a hidden UIView where you want the UIView to end. You can use the preview in xcode to make sure both UIViews are where you want them to be. After that, hide the ending UIView and swap the layout constraints.

There is a podfile for swapping layout constrains called SBP if you don't want to write it yourself.

Here's a tutorial.

于 2015-11-15T21:26:52.193 に答える