简体   繁体   English

UICollisionBehavior仅在第一次添加后才能工作

[英]UICollisionBehavior only works after adding the first time

My app has a custom UIButton with two subviews that look like they're hanging by strings. 我的应用程序有一个带有两个子视图的自定义UIButton,两个子视图看起来像是按字符串悬挂的。 When the user taps the button, one gets pulled back, then collides with the other. 当用户点击按钮时,其中一个被拉回,然后与另一个碰撞。 This is working for me on the first collision, but when I go to tap a second time, the item doesn't get pulled back. 这在第一次碰撞时为我工作,但是当我第二次点击时,该物品不会被拉回。

On the first call to layoutSubviews() , I add both views to a UIGravityBehavior , and I give them each their own UIAttachmentBehavior . 在第一次调用layoutSubviews() ,我将两个视图都添加到UIGravityBehavior ,并给它们各自分配了自己的UIAttachmentBehavior On touchesBegan , I add a collision adjacent to the view that needs to get pulled back, and then either on touchesEnded or after a timer if it was a tap and not a long press, I remove the collision that was holding it in place. touchesBegan ,我在需要拉回的视图旁边添加了一个碰撞,然后在touchesEnded或计时器之后(如果是轻按而不是长按),则删除了将其固定在适当位置的碰撞。

I store that collision in an instance variable (so it's retained), and create it like so: 我将该碰撞存储在一个实例变量中(因此将其保留),并按如下方式创建它:

    holdCollision = UICollisionBehavior(items: [theView])
    holdCollision.addBoundaryWithIdentifier("boundary suspending item in air",
        forPath: UIBezierPath(rect: holdCollisionViewFrame))

If I construct the UICollisionBehavior once and then call addBehavior and removeBehavior with it multiple times, it only works the first time. 如果我构建UICollisionBehavior一次,然后调用addBehaviorremoveBehavior它多次,它仅适用第一次。 If I construct it from scratch on each tap, it always works. 如果我在每次点击时从头开始构建它,它将始终有效。 What could be causing this? 是什么原因造成的?

Update: I came up with a sample project for reporting to Apple. 更新:我想出了一个向Apple报告的示例项目。 You can reproduce in a clean single-view iOS project. 您可以在干净的单视图iOS项目中重现。 Add two views, one over the other, and link to the outlets at the top. 添加两个视图,一个在另一个视图上,然后链接到顶部的出口。 The top view needs to be large enough that it overlaps with the bottom one at first launch, or you wont' see the problem. 顶视图必须足够大,以使其在首次启动时与底视图重叠,否则您将看不到问题。 Add two buttons and connect to the actions at the bottom. 添加两个按钮并连接到底部的操作。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var topView: UIImageView!
    @IBOutlet weak var bottomView: UIView!

    var animator: UIDynamicAnimator!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()

        animator = UIDynamicAnimator(referenceView: view)

        let gravity = UIGravityBehavior(items: [topView])
        animator.addBehavior(gravity)

        var anchor = view.center
        anchor.y -= 100

        let attachment = UIAttachmentBehavior(item: topView, attachedToAnchor: anchor)
        animator.addBehavior(attachment)

        collision = UICollisionBehavior(items: [topView])
        collision.addBoundaryWithIdentifier("bottom boundary", forPath: UIBezierPath(rect: bottomView.frame))
    }

    @IBAction func addCollision() {
        NSLog("boundaries: \(collision.boundaryIdentifiers)")
        animator.addBehavior(collision)
    }

    @IBAction func removeCollision() {
        animator.removeBehavior(collision)
    }
}

It looks like after I remove the holdCollision from the UIDynamicAnimator , it loses its list of boundaries. UIDynamicAnimator删除holdCollision UIDynamicAnimator ,它似乎失去了边界列表。 That seems like a bug, and I reported it as such. 那似乎是一个错误,我就这样报告了。 It's rdar://23593048 if anyone would like to dupe. 如果有人要欺骗, 则为rdar:// 23593048

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

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