autolayout 是苹果为UI设计添加的自动布局的新方法,具体可看这个文档
http://www.cocoachina.com/ios/20151021/13825.html
http://www.cocoachina.com/ios/20151023/13742.html
添加好的部分代码写好了 下不下去了
import UIKit
//定义椭圆类
class Ellipse:UIView{
override var collisionBoundsType:UIDynamicItemCollisionBoundsType{
return .Ellipse
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
//创建UIdynaicAnimator的实例
var animator = UIDynamicAnimator(referenceView: view)
//添加视图
let square = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
square.backgroundColor = UIColor.blueColor()
view.addSubview(square)
let ellipse = Ellipse(frame:CGRect(x: 0, y: 0, width: 100, height: 100))
ellipse.backgroundColor = UIColor.yellowColor()
ellipse.layer.cornerRadius = 50
self.view.addSubview(ellipse)
//给view添加的两个行为
let items = [square,ellipse]
let gravity = UIGravityBehavior(items: items)
animator.addBehavior(gravity)
//添加重力感应模型
let noiseField = UIFieldBehavior.noiseFieldWithSmoothness(1.0, animationSpeed: 0.5)
noiseField.addItem(square)
noiseField.addItem(ellipse)
noiseField.strength = 0.5
animator.addBehavior(noiseField)
//再添加另外一个行为
let collision = UICollisionBehavior(items: items)
collision.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsets(top: 20, left: 5, bottom: 5, right: 5))
animator.addBehavior(collision)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}