简体   繁体   中英

Swift 2. UILabel collision between objects doesn't work

I'm using Swift 2 and iOS 9. When I enter something into textInputField and hit the plus button, a bubble should fall down with the entered text inside. I'm using a UILabel for this. Everything works so far, including behaviors like gravity, elasticity and collision.

Problem:

The bubbles don't collide among themselves. They always fall through each other.

Thank you so much!

Code:

var tags = [String]()
var bubbles = [Bubble]()


@IBAction func addTag(sender: AnyObject) {

    let xPosition : CGFloat = CGFloat( arc4random_uniform(200))+20
    var input = inputText.text

    if (input?.characters.count < 3) {

        let alert = UIAlertView(title: "Achtung", message: "Dein Tag muss mindestens Zeichen lang sein.", delegate: self, cancelButtonTitle: "OK")

        alert.show()

    } else {

        let tag = Bubble()
        tag.bubble(input!)
        tag.animator = UIDynamicAnimator(referenceView: view)
        tag.label.text =  input
        view.addSubview(tag.label)
        tag.gravity()


        tags.append(input!)
        bubbles.append(tag)
        inputText.text = ""


        print("\(tags)")



    }

Code from the Bubble:

var label = UILabel()

var animator = UIDynamicAnimator()



//magenta
let color = UIColor(red: 229.0/255, green: 0.0/255, blue: 81.0/255, alpha: 1.0)

//größe
let size : CGFloat = 100

// set yPosition to be a random number between 20.0 and 220.0
let xPosition : CGFloat = CGFloat( arc4random_uniform(200))+20
//        let bubble = UIView()

func bubble(let name: String){


    //magenta
    let color = UIColor(red: 229.0/255, green: 0.0/255, blue: 81.0/255, alpha: 1.0)

    //size
    let size : CGFloat = 100

    // set yPosition to be a random number between 20.0 and 220.0
    let xPosition : CGFloat = CGFloat( arc4random_uniform(200))+20
    //        let bubble = UIView()

    label.frame = (CGRect(x: xPosition, y: 320, width: size, height: size))
    label.layer.cornerRadius = label.frame.height/2
    label.layer.masksToBounds = true
    label.backgroundColor = color
    label.textColor = UIColor.whiteColor()

    label.text = name.uppercaseString
    label.textAlignment = .Center
    label.font = UIFont.boldSystemFontOfSize(12)

}


func gravity(){
    let gravity = UIGravityBehavior(items: [label])
    animator.addBehavior(gravity)
    let collision = UICollisionBehavior(items: [label])
    collision.translatesReferenceBoundsIntoBoundary = true
    animator.addBehavior(collision)
    let behavior = UIDynamicItemBehavior(items: [label])
    behavior.elasticity = 0.35
    animator.addBehavior(behavior)

}

From the UICollisionBehavior docs :

A collision behavior confers, to a specified array of dynamic items , the ability of those items to engage in collisions with each other and with the behavior's specified boundaries.

And:

a collision behavior's items can collide with each other and with any boundaries you've specified for the behavior.

However, you are only specifying one item:

let collision = UICollisionBehavior(items: [label])

label will not collide with other objects… because you are not specifying any other objects for it to collide with.

You need to add a collision behavior object with all of the items that should collide :

UICollisionBehavior(items: [label, label2, label3])

You'll need to do this farther up, when you have references to all of the "bubbles" (probably near bubbles.append(tag) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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