繁体   English   中英

结合UIAttachmentBehavior和UICollisionBehavior

[英]Combining UIAttachmentBehavior and UICollisionBehavior

使用UIKit Dynamics我希望以一种方式组合UIAttachmentBehavior和UICollisionBehavior,以便用户可以拖动视图(使用UIPanGestureRecognizer )但不留下某个区域。

当用户/ UIView与碰撞行为的边界碰撞时,问题就出现了,因为那时不可能进行垂直移动。

当IE与边界区域的左侧发生碰撞时,其中一个被“卡住”并且无法向上或向下移动,恰到好处。 将UIView拖回到用于去那里的确切路径上。

任何关于UIDynamicItemBehavior在其中工作的帮助都非常受欢迎(尝试过弹性,摩擦和阻力,但无济于事)。

在此输入图像描述

我认为你以错误的方式实现了UIPanGestureRecognizer 请尝试以下示例。 只需创建一个新项目并将此代码粘贴到ViewController中。 如果需要缩小可拖动区域,可以使用dragView功能。

import UIKit

class ViewController: UIViewController, UICollisionBehaviorDelegate {

    var collision: UICollisionBehavior!
    var animator: UIDynamicAnimator!

    override func viewDidLoad() {
        super.viewDidLoad()

        let container = UIView(frame: CGRect(x: 30, y: 60, width: 300, height: 500))
        self.view.addSubview(container)
        container.backgroundColor = .gray

        self.animator = UIDynamicAnimator(referenceView: container);
        //self.animator.setValue(true, forKey: "debugEnabled")

        let greenBox = UIView(frame: CGRect(x: 60, y: 240, width: 50, height: 50))
        greenBox.backgroundColor = .green
        container.addSubview(greenBox)

        let blueBox = UIView(frame: CGRect(x: 10, y: 10, width: 50, height: 50))
        blueBox.backgroundColor = .blue
        container.addSubview(blueBox)

        let redBox = UIView(frame: CGRect(x: 200, y: 300, width: 50, height: 50))
        redBox.backgroundColor = .red
        container.addSubview(redBox)

        self.collision = UICollisionBehavior(items: [greenBox, blueBox, redBox])
        self.collision.translatesReferenceBoundsIntoBoundary = true
        self.collision.collisionMode = .everything
        self.animator.addBehavior(self.collision)
        self.collision.collisionDelegate = self


        let c = UIDynamicItemBehavior(items: [redBox])
        c.density = 10000
        self.animator.addBehavior(c)

        let noRotation = UIDynamicItemBehavior(items: [greenBox, blueBox])
        noRotation.allowsRotation = false
        self.animator.addBehavior(noRotation)

        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.dragView(_:)))
        greenBox.isUserInteractionEnabled = true
        greenBox.addGestureRecognizer(panGesture)

        let panGesture2 = UIPanGestureRecognizer(target: self, action: #selector(self.dragView(_:)))
        blueBox.isUserInteractionEnabled = true
        blueBox.addGestureRecognizer(panGesture2)

    }


    @objc func dragView(_ sender:UIPanGestureRecognizer){

        if let viewDrag = sender.view {
            self.view.bringSubview(toFront: viewDrag)
            let translation = sender.translation(in: self.view)
            viewDrag.center = CGPoint(x: viewDrag.center.x + translation.x, y: viewDrag.center.y + translation.y)
            sender.setTranslation(CGPoint.zero, in: self.view)
            animator.updateItem(usingCurrentState: viewDrag)
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM