iOS Core Animation详解(四)AutoLayout中的动画_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:39:21
原创
1422人浏览过

前言:autolayout定义了view的位置,也就是说,在auto layout的工程里,如果不修改约束本身,在视图重新绘制的时候,还会回到最开始的位置。autolayout中的动画与视图的位置和大小有关。

先看看效果

实现过程

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

拖拽Imageview以及Constraint为Outlet

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

立即学习前端免费学习笔记(深入)”;


对应代码

 @IBOutlet weak var imageview: UIImageView! @IBOutlet weak var yConstraints: NSLayoutConstraint!
登录后复制

在viewDidload中设置imageview的初始状态

四维时代AI开放平台
四维时代AI开放平台

四维时代AI开放平台

四维时代AI开放平台 66
查看详情 四维时代AI开放平台
 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
登录后复制

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号