iOSCoreAnimation详解(四)AutoLayout中的动画_html/css_WEB-ITnose
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOSCoreAnimation详解(四)AutoLayout中的动画_html/css_WEB-ITnose,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3701字,纯文字阅读大概需要6分钟。
内容图文
![iOSCoreAnimation详解(四)AutoLayout中的动画_html/css_WEB-ITnose](/upload/InfoBanner/zyjiaocheng/403/2188cc5035a54973b7f18ad610c03911.jpg)
先看看效果
实现过程
在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
内容总结
以上是互联网集市为您收集整理的iOSCoreAnimation详解(四)AutoLayout中的动画_html/css_WEB-ITnose全部内容,希望文章能够帮你解决iOSCoreAnimation详解(四)AutoLayout中的动画_html/css_WEB-ITnose所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。