ios – 结合UIAttachmentBehavior和UICollisionBehavior

ios – 结合UIAttachmentBehavior和UICollisionBehavior,第1张

概述使用UIKit Dynamics我希望以一种方式组合UIAttachmentBehavior和UICollisionBehavior,以便用户可以拖动视图(使用UIPanGestureRecognizer)但不留下某个区域. 当用户/ UIView与碰撞行为的边界碰撞时,问题就出现了,因为那时不可能进行垂直移动. I.E.当与界限区域的左侧碰撞时,一个人在那里“卡住”并且不能向上或向下移动,恰好是 使用UIKit Dynamics我希望以一种方式组合UIAttachmentBehavior和uicollisionbehavior,以便用户可以拖动视图(使用UIPanGestureRecognizer)但不留下某个区域.

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

I.E.当与界限区域的左侧碰撞时,一个人在那里“卡住”并且不能向上或向下移动,恰好是正确的.将UIVIEw拖回到用于去那里的确切路径上.

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

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

import UIKitclass 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,height: 50))        blueBox.backgroundcolor = .blue        container.addSubvIEw(blueBox)        let redBox = UIVIEw(frame: CGRect(x: 200,y: 300,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)        }    }}
总结

以上是内存溢出为你收集整理的ios – 结合UIAttachmentBehavior和UICollisionBehavior全部内容,希望文章能够帮你解决ios – 结合UIAttachmentBehavior和UICollisionBehavior所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1027201.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存