1. SB放上俩 imageview,拖线成类属性

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var box2: UIImageView!
    @IBOutlet weak var box1: UIImageView!
    
    //1.s创建仿真器
       private lazy var animator: UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置背景颜色图
        view.backgroundColor = UIColor(patternImage: UIImage.init(named: "background") ?? UIImage())
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let point = touches.first?.location(in: view)
        snap(point: point ?? CGPoint.zero)
        
        gravity()
        collision()
        push()
    }
    
    ///重力
    func gravity() {
        
        //2.创建仿真行为
        let behavior = UIGravityBehavior(items: [box1])
        
        //重力方向,重力的力度
//        behavior.gravityDirection = CGVector(dx: 10, dy: -10)
        
//        //角度
//        behavior.angle =  -CGFloat.pi
//
//        //量级:速度
//        behavior.magnitude = 5
        
        //设置角度和量级
        behavior.setAngle( CGFloat.pi/2, magnitude: 5)
        
        //3. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
    
    
    /// 碰撞
    func collision() {
        
        //2.创建仿真行为
        let behavior = UICollisionBehavior(items: [box1, box2])
        
        //添加碰撞边界: view的bounds
        behavior.translatesReferenceBoundsIntoBoundary = true

//        items: 只碰撞元素
//        boundaries:只碰撞边界
//        everything:任何东西
        //碰撞模式
        behavior.collisionMode = UICollisionBehavior.Mode.everything
        
        //设置边界的内边距
//        behavior.setTranslatesReferenceBoundsIntoBoundary(with: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
        
        let str :  NSString = "123" // NSString 遵循NSCopying
        let str1 : String = "123"   //String 没有遵循NSCopying
        //添加一个边界
//        behavior.addBoundary(withIdentifier: str, from: CGPoint(x: 0, y: 500), to: CGPoint(x: view.frame.size.width, y: 300))
        
        
        //设置代理
        behavior.collisionDelegate = self
        //3. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
    

    /// 创建捕捉行为
    func snap(point:CGPoint){
        //FIXME:如果想要多次执行p捕捉行为,必须先移除添加捕捉行为
        animator.removeAllBehaviors()
        
        let behavior = UISnapBehavior(item: box2, snapTo: point)
        
        //动画流畅度
        behavior.damping = 1
        
        animator.addBehavior(behavior)
    }

    func push(){
        
        // continuous:一直推
        // instantaneous:推一次
        //1.创建推行为
        let behavior = UIPushBehavior(items: [box1], mode: UIPushBehavior.Mode.instantaneous)
        
        //设置方向
        behavior.pushDirection = CGVector(dx: 0, dy: -8)
        
        //2. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
    
}


extension ViewController:UICollisionBehaviorDelegate{
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item1: UIDynamicItem, with item2: UIDynamicItem, at p: CGPoint){
        print("开始碰撞 元素 - 元素", p)
    }
    
    func collisionBehavior(_ behavior: UICollisionBehavior, endedContactFor item1: UIDynamicItem, with item2: UIDynamicItem){
        print("结束碰撞 元素 - 元素")
    }
    
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint){
        print("开始碰撞 元素 - 边界", p)
    }
    
    func collisionBehavior(_ behavior: UICollisionBehavior, endedContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?){
        print("结束碰撞 元素 - 边界")
    }
}

  

posted on 2019-01-22 09:44  懂事长qingzZ  阅读(268)  评论(0编辑  收藏  举报