[iOS]NSLayoutConstraint动画改变约束

[iOS]NSLayoutConstraint动画改变约束

一直都是直接调用.constant修改约束,没什么动画需求。
之前做地图浮层时,需求要求浮层能动画的伸缩,于是查询了一下constant如何实现动画改变。
基本上和动画更新UIView的frame一样,区别在这里父视图需要调用layoutIfNeeded方法。

#import 

@interface XZUserCenterViewController : UIViewController
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;

@end

@implementation XZUserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [UIView animateWithDuration:0.3 animations:^{
        _widthConstraint.constant = (235/375.0)*[UIScreen mainScreen].bounds.size.width;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

@end
示意图:


你可能感兴趣的:([iOS]学习笔记)