gpt4 book ai didi

ios - UICollisionBehavior - View 穿过边界

转载 作者:IT王子 更新时间:2023-10-29 05:17:41 25 4
gpt4 key购买 nike

作为实现 UICollisionBehaviour 的一部分,我为屏幕边缘设置了边界。然后我添加了一些 View ,最后将 UIPanGestureRecognizer 附加到其中一个。

现在我可以使用可拖动 View 来插入较小的 View 。

Hammer-time!

问题:如果我将一个较小的 View 转角并继续将其推向屏幕边缘,它最终会滑过边界并被困在另一侧。我用来击打和插入其他 View 的“锤 subview ”也会陷入边界。 IE。它被卡在/不可拖动到屏幕的一侧。

我做了一个非常小的例子,看看当我只有很少的 View 并且没有冲突的行为时,我是否可以重现它, View 仍然穿过边界。要么 UIDynamics 不能处理太多,要么(更有可能)我以某种方式配置错误。

下面的小例子有奇怪的行为:

class ViewController: UIViewController {

var animator: UIDynamicAnimator?
var collisionBehaviour: UICollisionBehavior?
var panBehaviour: UIAttachmentBehavior?

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
setup()
}

func setup() {
//setup collisionBehaviour and animator
collisionBehaviour = UICollisionBehavior()
collisionBehaviour!.collisionMode = UICollisionBehaviorMode.allZeros
animator = UIDynamicAnimator(referenceView: view)

//add boundaries
collisionBehaviour!.addBoundaryWithIdentifier("verticalMin", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(0, CGRectGetHeight(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("verticalMax", fromPoint: CGPointMake(CGRectGetMaxX(view.frame), 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetHeight(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("horizontalMin", fromPoint: CGPointMake(0, CGRectGetMaxY(view.frame)), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("horizontalMax", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), 0))

// collisionBehaviour!.translatesReferenceBoundsIntoBoundary = true // same effect as the above boundaries
//setup up some round views to push around
for i in 0..<5 {
let ball = UIView(frame: CGRectMake(0, 30, 50, 50))
ball.center = view.center
ball.backgroundColor = UIColor.greenColor()
ball.layer.cornerRadius = CGRectGetWidth(ball.frame) * 0.5
view.addSubview(ball)
collisionBehaviour!.addItem(ball)
}

//setup a hammer view which can be dragged and used to squeze the ball views of the screen
let hammer = UIView(frame: CGRectMake(0, 0, 100, 100))
hammer.backgroundColor = UIColor.redColor()
view.addSubview(hammer)
collisionBehaviour!.addItem(hammer)

let noRotationBehaviour = UIDynamicItemBehavior(items: [hammer])
noRotationBehaviour.allowsRotation = false
animator!.addBehavior(noRotationBehaviour)

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("handlePan:"))
hammer.addGestureRecognizer(panGestureRecognizer)

//"start" the collision detection
animator!.addBehavior(collisionBehaviour!)
}

//Move the hammer around
func handlePan(recognizer: UIPanGestureRecognizer) {
if let view = recognizer.view {
let location = recognizer.locationInView(self.view)
switch recognizer.state {
case .Began:
panBehaviour = UIAttachmentBehavior(item: view, attachedToAnchor: location)
animator!.addBehavior(panBehaviour!)
println("begin")
case .Changed:
panBehaviour!.anchorPoint = location
println("change \(location)")
case .Ended:
println("ended")
animator!.removeBehavior(panBehaviour!)
default:
println("done")
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

感谢您提供的任何帮助。

最佳答案

好消息:您没有做错任何事。坏消息:这是预期的行为。你的锤子将 View 推向引用边界,直到它最终突破,因为你的模型的物理学说它应该这样做。引用边界不是不能跨越的边界,它只是定义了你的物理规则始终适用的区域。

这并没有真正记录下来,但 UICollisionBehavior 的页面指出:

When setting the initial position for a dynamic item, you must ensure that its bounds do not intersect any collision boundaries. The animation behavior for such a misplaced item is undefined.

这只是部分正确,在您的情况下(与我的情况一样)如果项目在初始化后超出边界,它的行为也是未定义的。

我试图在 View 的右侧安排一组球。最右边的球下面有一个 anchor 。黄色 View 是引用 View ,一切都很好...... this works

但是,当我添加更多球到它们不再适合时,它们会开始弹出。事实上,图像右上角的那个从底部中心弹出并逆时针围绕引用 View 滚动到 anchor 附近。 this is broken

更新:对于解决方案,您可以设置 collisionBehaviors 的 collisionDelegate 并捕获碰撞的开始和结束,然后将您的 View 移回边界并远离您的锤子,使其看起来像是逃脱了。

如您所知,translatesReferenceBoundsIntoBoundary 等同于一组 addBoundaryWithIdentifier 调用。

使用:

collisionBehaviour!.translatesReferenceBoundsIntoBoundary = true 

同于:

//add boundaries
collisionBehaviour!.addBoundaryWithIdentifier("verticalMin", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(0, CGRectGetHeight(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("verticalMax", fromPoint: CGPointMake(CGRectGetMaxX(view.frame), 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetHeight(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("horizontalMin", fromPoint: CGPointMake(0, CGRectGetMaxY(view.frame)), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame)))
collisionBehaviour!.addBoundaryWithIdentifier("horizontalMax", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), 0))

关于ios - UICollisionBehavior - View 穿过边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com