首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结合使用UIAttachmentBehavior和UICollisionBehavior

结合使用UIAttachmentBehavior和UICollisionBehavior
EN

Stack Overflow用户
提问于 2015-06-21 00:51:58
回答 1查看 471关注 0票数 4

使用UIKit动态,我想以某种方式组合UIAttachmentBehavior和UICollisionBehavior,这样用户就可以拖动视图(使用UIPanGestureRecognizer),但不会离开某个区域。

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

也就是说,当与边界区域的左侧碰撞时,一个人被“卡住”在那里,不能向上或向下移动,恰到好处。将UIView拖回到原来的路径上是可行的。

我们非常感谢UIDynamicItemBehavior解决这个问题的任何帮助(尝试过弹性、摩擦力和阻力,但都无济于事)。

EN

回答 1

Stack Overflow用户

发布于 2017-11-19 09:27:37

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

代码语言:javascript
复制
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)
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30956577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档