UIKit 力学教程

import UIKit

 

class ViewController: UIViewController {

    

    var animator: UIDynamicAnimator?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        let square = UIView(frame: CGRect(x: 120, y: 50, width: 80, height: 80))

        square.backgroundColor = UIColor.black

        self.view.didAddSubview(square)

        

        let barrier = UIView(frame: CGRect(x: 0, y: 350, width: 140, height: 25))

        barrier.backgroundColor = UIColor.red

        self.view.addSubview(barrier)

        

        //1、创建运动管理

        animator = UIDynamicAnimator(referenceView: self.view)

//

//        //2,4 创建运动行为 使用的时候, 运动行为添加 运动物体

        let gravity = UIGravityBehavior(items: [square])

//

//        //添加不可见边界

        let right = CGPoint(x: barrier.frame.origin.x + barrier.frame.size.width, y: barrier.frame.origin.y)

//

        let collision = UICollisionBehavior(items: [square])

        

        collision.translatesReferenceBoundsIntoBoundary = true

        

        collision.addBoundary(withIdentifier: "aa" as NSCopying, from: barrier.frame.origin, to: right)

        

        //3 运动管理 添加 运动行为

//        animator?.addBehavior(gravity)

        animator?.addBehavior(collision)

    }

    

}

posted @ 2016-11-23 08:01  不抛弃,不放弃  阅读(288)  评论(0编辑  收藏  举报