原创blog。转载请注明出处

blog.csdn.net/hello_hwc

欢迎关注我的iOS SDK具体解释专栏

http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html


前言:AutoLayout定义了View的位置,也就是说,在Auto Layout的project里,假设不改动约束本身,在视图又一次绘制的时候。还会回到最開始的位置。AutoLayout中的动画与视图的位置和大小有关。


先看看效果


实现过程

在Storyboard上拖拽一个UIImageview。设置约束为:水平垂直正中心,大小非常定100*100

拖拽Imageview以及Constraint为Outlet

注意拖拽Y相关的约束,也就是这个



相应代码

 @IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var yConstraints: NSLayoutConstraint!

在viewDidload中设置imageview的初始状态

 yConstraints.constant = yConstraints.constant - CGRectGetHeight(UIScreen.mainScreen().bounds)/2
self.imageview.alpha = 0.0;
self.imageview.transform = CGAffineTransformMakeScale(0.1, 0.1)
self.view.layoutIfNeeded();

ViewWillAppear中创建动画

 yConstraints.constant = yConstraints.constant + CGRectGetHeight(UIScreen.mainScreen().bounds)/2
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.imageview.alpha = 1.0
self.imageview.transform = CGAffineTransformIdentity
self.view.layoutIfNeeded()
})

原理

原理比較简单。就是利用改动约束NSLayoutConstraint中的属性constant。然后调用layoutIfNeeded来实现动画。注意。属性multiplier眼下(iOS 8.4)还是仅仅读的,不能改动。可是能够通过关系view1.property = view2.property * multiplier + constant进行转换。


纯代码的AutoLayout动画

上述动画用纯代码实现

class ViewController: UIViewController {

    var imageview:UIImageView?
weak var yConstraint:NSLayoutConstraint? override func viewDidLoad() {
super.viewDidLoad()
//加入Imageview
let image = UIImage(named: "1_hello_hwc.jpg")
imageview = UIImageView(image: image)
self.imageview? .setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.imageview!) //创建约束,定义最開始的位置 let hC = NSLayoutConstraint(item:self.view, attribute:NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.imageview, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0)
let vC = NSLayoutConstraint(item:self.view, attribute:NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.imageview, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0)
yConstraint = vC;
yConstraint!.constant = yConstraint!.constant - CGRectGetHeight(UIScreen.mainScreen().bounds)/2 let widthC = NSLayoutConstraint(item:self.imageview!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100)
let widthH = NSLayoutConstraint(item:self.imageview!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100)
self.view.addConstraints([hC,vC,widthC,widthH]) //定义最開始的状态
self.imageview? .alpha = 0.0;
self.imageview? .transform = CGAffineTransformMakeScale(0.1, 0.1);
self.view.layoutIfNeeded()
}
override func viewWillAppear(animated: Bool) {
yConstraint!.constant = yConstraint!.constant + CGRectGetHeight(UIScreen.mainScreen().bounds)/2
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.imageview!.alpha = 1.0
self.imageview!.transform = CGAffineTransformIdentity
self.view.layoutIfNeeded()
})
} }

定位Constraints

设置属性identifier

yConstraint.identifier = "identifier"

然后在过滤。定义到这个Constraints,

 let constraint =  filter(self.view.constraints() as! [NSLayoutConstraint], { (constraint:NSLayoutConstraint) -> Bool in
return constraint.identifier == "identifier"
}).first

iOS Core Animation具体解释(四)AutoLayout中的动画的更多相关文章

  1. iOS - Core Animation 核心动画

    1.UIView 动画 具体讲解见 iOS - UIView 动画 2.UIImageView 动画 具体讲解见 iOS - UIImageView 动画 3.CADisplayLink 定时器 具体 ...

  2. iOS Core Animation 简明系列教程

    iOS Core Animation 简明系列教程  看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽 ...

  3. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  4. iOS——Core Animation 知识摘抄(四)

    原文地址http://www.cocoachina.com/ios/20150106/10840.html 延迟解压 一旦图片文件被加载就必须要进行解码,解码过程是一个相当复杂的任务,需要消耗非常长的 ...

  5. IOS Core Animation Advanced Techniques的学习笔记(四)

    第五章:Transforms   Affine Transforms   CGAffineTransform是二维的     Creating a CGAffineTransform   主要有三种变 ...

  6. iOS——Core Animation 知识摘抄(二)

    阴影 主要是shadowOpacity .shadowColor.shadowOffset和shadowRadius四个属性 shadowPath属性 我们已经知道图层阴影并不总是方的,而是从图层内容 ...

  7. IOS Core Animation Advanced Techniques的学习笔记(五)

    第六章:Specialized Layers   类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradient ...

  8. iOS——Core Animation 知识摘抄(三)

    原文地址:http://www.cocoachina.com/ios/20150105/10827.html CAShapeLayer CAShapeLayer是一个通过矢量图形而不是bitmap来绘 ...

  9. iOS——Core Animation 知识摘抄(一)

    本文是对http://www.cocoachina.com/ios/20150104/10814.html文章的关键段落的摘抄,有需要的看原文 CALayer和UIView的关系: CALayer类在 ...

随机推荐

  1. SQL Server单表已700w+将普通表转换成分区表1

    最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤.   一.创建文件组 ...

  2. Metasploit学习笔记(博主推荐)

    不多说,直接上干货! 连接后台的数据库,当然不是必须品. 连接数据库的好处:1.可以攻击和扫描的结果,保存起来      2.将一些搜索结果做个缓存 默认数据库是postgresql. 同时要注意的是 ...

  3. canvas指定的宽高写在行间和写在style里面的区别?

    上代码,指定的canvas宽高都一样,线条的粗细都是5px 1.宽:400:高:300:直接写在<canvas>里的效果: 2.删除<canvas>里的宽高,宽:400:高:3 ...

  4. <Sicily>Brackets Matching

    一.题目描述 Let us define a regular brackets sequence in the following way: Empty sequence is a regular s ...

  5. JavaScript进阶之原型链

    对象 function f1(){ }; typeof f1 //"function"函数对象 var o1 = new f1(); typeof o1 //"objec ...

  6. [JSOI2007]文本生成器(AC自动机+DP)

    题意 给你n个串.问有多少长度为m的串使得这n个串至少在其中出现过一次.输出答案膜10007意义下的结果. (n<=100,每个串的长度<=100) 题解 在AC自动机上跑DP. 用到一个 ...

  7. vuex 闲置状态重置方案

    前言 大型单页应用(后面都是指spa),我们往往会通过使用状态管理器 vuex 去解决组件间状态共享与状态传递等问题.这种应用少则几十个单页,多则上百个单页.随着路由的频繁切换,每个路由对应的 vue ...

  8. Unity 编辑器学习(三)之 Light & Baked

    上一篇博客已经详细的介绍GI了,接下来我们讲点实际的,怎么烘焙场景及注意事项. 一.Light Property: Function: Type 当前灯光的类型.有四种类型:Directional,  ...

  9. ECNUOJ 2142 放书

    放书 Time Limit:1000MS Memory Limit:65536KBTotal Submit:409 Accepted:173 Description  你要把一叠书放进一些箱子里面,为 ...

  10. spring mvc 防止重复提交表单的两种方法,推荐第二种

    第一种方法:判断session中保存的token 比较麻烦,每次在提交表单时都必须传入上次的token.而且当一个页面使用ajax时,多个表单提交就会有问题. 注解Token代码: package c ...