码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-10 09:20:52      阅读:226      评论:0      收藏:0      [点我收藏+]

标签: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

点击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,并且带有动画效果.

技术分享

原文地址:http://blog.csdn.net/yangbingbinga


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

标签:autolayout动画   frame动画   修改约束动画效果   

原文地址:http://blog.csdn.net/yangbingbinga/article/details/43680141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!