荼菜的iOS笔记--UIView的几个Block动画

二
前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会觉得麻烦,不用慌,苹果工程师爸爸们已经为我们给UIView封装好了一些coreAnimationBlock,足以满足平时的动画需求。

  • 动画Block1
      
      
1
2
3
4
5
6
7
8
9
10
      
      
/*
参数1: Duration: 动画持续时间
参数2: delay: 延迟时间
参数3: options: 枚举值 动画的效果类型
*/
[UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
ws.MyLabel.frame = CGRectMake(100, 100, 100, 100);
} completion:^(BOOL finished) {
NSLog(@"结束了%d", finished);
}];
  • 动画Block2-(模拟弹簧弹跳的效果)
      
      
1
2
3
4
5
6
7
8
9
10
11
      
      
/*
Spring: 模拟弹簧弹跳的效果
参数: Damping:阻尼 0-1 阻尼越小动画越明显
参数: initialSpringVelocity : 动画初始变化速度
参数: options 转变的风格 枚举值
*/
[UIView animateWithDuration:10 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:15 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
ws.MyLabel.center = CGPointMake(self.view.center.x, 100);
} completion:^(BOOL finished) {
NSLog(@"弹簧效果结束");
}];
  • 动画Block3-(关键帧动画)也就是里面有好几个动画进行转变
      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      
      
/*
Duration:持续时间
delay: 延迟时间
options: 枚举值 动画的风格
*/
[UIView animateKeyframesWithDuration:3 delay:0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
/*
参数1: RelativeStartTime: 相对的开始时间
参数2: relativeDuration:相对持续时间
*/
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
ws.MyLabel.center = self.view.center;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.3 animations:^{
ws.MyLabel.frame = CGRectMake(100, 100, 100, 100);
}];
[UI 大专栏   荼菜的iOS笔记--UIView的几个Block动画View addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.3 animations:^{
ws.MyLabel.frame = CGRectMake(100, 400, 100, 100);
}];
} completion:^(BOOL finished) {
NSLog(@"开始了吗?已经结束了。。");
}];
  • Masonry动画更新约束
    1.有时不适用
            
            
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
            
            
    [ws.allWarpView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(ws.view.mas_left);
    make.right.equalTo(ws.view.mas_right);
    make.top.equalTo(ws.view.mas_top);
    make.bottom.equalTo(ws.view.mas_bottom);
    }];
    // 告诉self.view约束需要更新
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
    [self.view layoutIfNeeded];
    }];

2.解决

      
      
1
2
3
4
5
6
7
8
      
      
   [self layoutIfNeeded];//如果其约束还没有生成的时候需要动画的话,就请先强制刷新后才写动画,否则所有没生成的约束会直接跑动画
    JCWeakSelf
    [UIView animateWithDuration:0.3 animations:^{ 
        [_keywordTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(@(255*RealScaleFactor));
        }];
        [ws layoutIfNeeded];//强制绘制
    }];


后话:附上之前写的文章,欢迎指正:
荼菜的iOS笔记–Core Animation 核心动画
荼菜的iOS笔记–一些实用功能。
荼菜的iOS笔记–UITableViewCell的各种操作(刷新、插入、删除、动画)
荼菜的iOS笔记–一张图记住所有git命令行操作。
荼菜的iOS笔记–一张图告诉你程序员需要知道的这些网站。
荼菜的iOS笔记–图片裁剪黑魔法。
荼菜的iOS笔记–iOS自动打包脚本(Python)
荼菜的iOS笔记–我的编码规范参考。
荼菜的iOS笔记–iOS基础优秀博客总结ToDoList
荼菜的iOS笔记–Xcode Tips
当然,我还写过诗。。。
光。

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12370790.html
今日推荐