ios - Swift 2. 对象之间的 UILabel 碰撞不起作用

标签 ios swift uilabel swift2 collision

我使用的是 Swift 2 和 iOS 9。当我在 textInputField 中输入内容并点击加号按钮时,应该会出现一个气泡,其中包含输入的文本。为此,我使用了 UILabel。到目前为止一切正常,包括重力、弹性和碰撞等行为。

问题:

气泡不会相互碰撞。他们总是互相跌倒。

非常感谢!

代码:

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)")
       
        
            
    }

气泡中的代码:

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)
  
}

最佳答案

来自 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.

还有:

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

但是,您仅指定一个项:

let collision = UICollisionBehavior(items: [label])

label 不会与其他对象发生碰撞...因为您没有指定任何其他对象来让它发生碰撞。

您需要添加一个碰撞行为对象,包含所有应该碰撞的项目:

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

当您引用了所有“气泡”(可能靠近 bubbles.append(tag))时,您需要进一步执行此操作。

关于ios - Swift 2. 对象之间的 UILabel 碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482635/

相关文章:

mysql - iOS 上开发人员提供的用户更新列表的方法

ios - 符号化 iOS 崩溃转储 : symbols not found

ios - uicontainerview(容器 View )内的多个 subview Controller

swift - 如何调整字典的值以快速阅读?

iOS/swift : Can't assign optional String to UILabel text property

ios - 向 UILabel 添加左/右水平填充

ios - 解析 UICollectionView 的查询不工作

dictionary - 在 Swift 中为可选字典赋值

swift - 为什么 Firebase Analytics 数据没有显示在 BigQuery 中?

iOS 标签不会在 Swift 中使用函数更新文本