美文网首页UI效果iOS学习征服iOS
iOS-Autolayout控件执行动画

iOS-Autolayout控件执行动画

作者: 西边雨 | 来源:发表于2017-02-22 14:04 被阅读337次

前言
在现在这个时候,手机屏幕越来越大,越来越多。手机屏幕从3.5->4-> 4.7 ->5.5,屏幕越来越大,适配这些也越来越麻烦,苹果给出了一个很好的解决方案 Autolayout。 目前Autolayout使用很广也有很多第三方的替代方案 例如Masonry(自己使用觉得很不错)。此处不多讲Autolayout使用,此处主要讲动画如何实现。


我们来看看当前这个动画,这个动画是我在目前负责的一个SDK中实现的。


2017-02-22 11_10_04.gif

现在很多打的公司都会做一些步数 里程的捐赠,这个是我 公益的项目,大家可以看下进度条的动画。

进度条都是根据Autolayout设置。
代码如下:

    [_progressAnimationView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@10);
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-56);
        make.top.equalTo(_titleLabel.mas_bottom).with.offset(5);
    }];
    
    [_progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@5);
        make.left.equalTo(self.progressAnimationView);
        make.right.equalTo(self.progressAnimationView);
        make.centerY.equalTo(self.progressAnimationView);
    }];

这里不做多解释,这里使用Masonry设置控件的Autolayout。

这里有使用了几个控件

progressAnimationView //用来承载动画
progressView //进度条
progressImageView //进度条上的指示点
progressCenterView //指示点应该存在的位置

我们来看看它们的布局关系:

Paste_Image.png Paste_Image.png

为何我们需要一个progressAnimationView?

根据描述苹果的Auto Layout Guide描述了autoLayout搞动画的基本方法,推荐的代码如下:

  [containerView layoutIfNeeded];
[UIView animateWithDuration:1.0 animations:^{
  // Make all constraint changes here
  [containerView layoutIfNeeded];
}];

可以看出做动画需要父View 去drewRect 中实现动画,用progressAnimationView的原因就是把进度条的动画View都当成子View然后去刷新布局开始做动画。

那我们开始实现动画吧 Do it

 [self.progressCenterView mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (self.progressView.progress > 0) {
               make.centerX.equalTo(self.progressView.mas_centerX).multipliedBy(2 * self.progressView.progress);//通过设置进度设置相对于progress进度的位置点
            }
            make.centerY.equalTo(self.progressView.mas_centerY);
 }];
  [self.progressImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_progressCenterView);
 }];

这个就是设置动画的核心地方,我们来看看全部代码

- (void)startProgressAnimationCompletion:(void (^)(void))finishBlcok{
    //在这调用一下updateConstraintsIfNeeded 在动画开始之前保证之前的约束更新UI完成。
    [self.progressAnimationView updateConstraintsIfNeeded];
    [UIView animateWithDuration:1 animations:^{
        [self.progressCenterView mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (self.progressView.progress > 0) {
                make.centerX.equalTo(self.progressView.mas_centerX).multipliedBy(2 * self.progressView.progress);
            }
            make.centerY.equalTo(self.progressView.mas_centerY);
        }];
        [self.progressImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_progressCenterView);
        }];
        [self.progressAnimationView layoutIfNeeded];
    } completion:^(BOOL finished) {
        if(finishBlcok){
            finishBlcok();
        }
    }];
}

然后跑起来,动画就开始了。

如有疑问可以直接留言,谢谢

接下来会分享下HealthKit 和 Coremotion 如何获取运动数据

相关文章

  • iOS-Autolayout控件执行动画

    前言在现在这个时候,手机屏幕越来越大,越来越多。手机屏幕从3.5->4-> 4.7 ->5.5,屏幕越来越大,适配...

  • flutter-实现一个简单的展开收起动画

    使用Tween动画,改变控件距左距离展开时,展示菜单控件,动画正向执行;收起后,动画反向执行,隐藏菜单控件;

  • Android动画卡顿问题

    父布局执行动画后,再给这个父布局里的子控件执行动画,会出现卡顿问题,解决办法就是clean掉父布局的动画,再执行子...

  • 自定义控件

    由组合控件和补间动画完成 注意的问题:1.等动画执行完才能再次点击2.由于是补间动画,动画旋转隐藏后,点击事件还在...

  • 2018-05-18

    11、用NSLayoutConstrain给控件做了约束以后,如何执行一定的UIView动画呢? // 高度约束,...

  • 有动画效果的登陆界面

    demo界面效果有点low??? 需要关注的技术点: 1.对控件的居中约束进行控制2.执行动画恢复控件的居中约束 ...

  • Android2

    做动画的时候要注意了 ,就是这个动画没有结束的时候不要执行下一个动画。做自定义控件就是要注意暴力点击的问题。关联j...

  • Android利用属性动画自定义倒计时控件

    本文介绍一下利用属性动画(未使用Timer,通过动画执行次数控制倒计时)自定义一个圆形倒计时控件,比较简陋,仅做示...

  • UIViewAnimationOptions的讲解

    UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将...

  • 动画·Animation

    简介: android中的动画分为两种补间动画和帧动画 一、补间动画 通过移动控件的位置,改变透明度,控件的角度等...

网友评论

本文标题:iOS-Autolayout控件执行动画

本文链接:https://www.haomeiwen.com/subject/aywpwttx.html