autolayout动画效果实现的几种方法

对于一个基于约束的布局视图,如何改变其值并且带有动画的特效,下面提供两种方法:

如下图,图中有一个很长的view和两个button,现在要求,改变view的高度,并且 两个Button随之上移,并且带有动画的效果!

如何实现?

方法一: 修改constant值,并且重新布局

整体代码

//// ViewController.m// AutoLayout2//// Created by yb on 15/2/9.// Copyright (c) 2015年 All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;//view1对应的高度约束@property (weak, nonatomic) IBOutlet UIView *view1;//很长的视图view1@property (weak, nonatomic) IBOutlet UIButton *button1;//按钮1@property (weak, nonatomic) IBOutlet UIButton *button2;//按钮2@end@implementation ViewController- (void)viewDidLoad{[super viewDidLoad];}//点击按钮做动画@end点击button1使用修改约束值做动画:

– (IBAction)constantModifyAnimation:(id)sender{self.height.constant-=100;[UIView animateWithDuration:1 animations:^{;[self.view layoutIfNeeded];} completion:^(BOOL finished) {;}];}使用约束修改值的做法来做动画非常的简单,只需要改变其constant值之后再,重新layout即可

方法2:点击button2使用frame改变view的高度和button的 .y值来 实现相同的效果:

– (IBAction)frameModifyAnimation:(id)sender{CGRect frame=self.view1.frame;CGRect frame1=self.button1.frame;CGRect frame2=self.button2.frame;frame.size.height-=100;frame1.origin.y-=100;frame2.origin.y-=100;[UIView animateWithDuration:1 animations:^{;self.view1.frame=frame;self.button1.frame=frame1;self.button2.frame=frame2;} completion:^(BOOL finished) {;}];}对比中我可以发现,使用frame修改约束时无法使与view有关的视图随着view的改变而移动,而使用约束的话,只需要简单的更改constant值即可;视图都是相对的变化.

使用frame改变view的frame对 底部的 按钮的位置没有影响,所以我们要改变view.frame.y值.

最终的效果都是这样的,view的高度减少了100,并且带有动画效果.

原文地址:

,深重如溺入蓝色的海洋,无法呼吸。

autolayout动画效果实现的几种方法

相关文章:

你感兴趣的文章:

标签云: