标签:autolayout动画 frame动画 修改约束动画效果
对于一个基于约束的布局视图,如何改变其值并且带有动画的特效,下面提供两种方法:
如下图,图中有一个很长的view和两个button,现在要求,改变view的高度,并且 两个Button随之上移,并且带有动画的效果!
如何实现?
方法一: 修改constant值,并且重新布局
整体代码
//
// ViewController.m
// AutoLayout2
//
// Created by yb on 15/2/9.
// Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga. 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
- (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的frame对 底部的 按钮的位置没有影响,所以我们要改变view.frame.y值.
最终的效果都是这样的,view的高度减少了100,并且带有动画效果.
原文地址:http://blog.csdn.net/yangbingbinga
标签:autolayout动画 frame动画 修改约束动画效果
原文地址:http://blog.csdn.net/yangbingbinga/article/details/43680141