ios - 快速碰撞边界不起作用

标签 ios swift uikit-dynamics

swift 2.0、xcode 7、iOS 9

意图:我想让这两个方 block 落到底部,并停留在屏幕底部。

现在发生了什么:没有错误阻止代码运行,重力工作正常但碰撞似乎被忽略了。从而导致两个物体从屏幕底部掉落。

注意:子类是 UIView 而不是 UIViewController 并且这个 View 是从 segue 访问的。

非常感谢!

代码:

import UIKit

class graphs: UIView {

//create two shapes
var greenSquare: UIView?
var redSquare: UIView?
var animator: UIDynamicAnimator?

override func drawRect(rect: CGRect) {

    var dimen = CGRectMake(25, 25, 60, 60)
    greenSquare = UIView(frame: dimen)
    greenSquare?.backgroundColor = UIColor.greenColor()

    dimen = CGRectMake(130, 25, 90, 90)
    redSquare = UIView(frame: dimen)
    redSquare?.backgroundColor = UIColor.redColor()

    //add them to the screen
    self.addSubview(greenSquare!)
    self.addSubview(redSquare!)

}

@IBAction func startbutton(sender: AnyObject) {
     //initialise the animator
    animator = UIDynamicAnimator()

    //colision
    let boundries = UICollisionBehavior(items:[greenSquare!,redSquare!])
    boundries.translatesReferenceBoundsIntoBoundary = true

    //add gravity
    let gravity = UIGravityBehavior(items: [greenSquare!, redSquare!])
    let direction = CGVectorMake(0.0, 1.0)
    gravity.gravityDirection = direction

    animator?.addBehavior(boundries)
    animator?.addBehavior(gravity)

   }
}

最佳答案

我通过将子类更改为 UIViewController 来修复它

为什么之前的代码不起作用?

这是新代码:

import UIKit

class DrawView: UIViewController {

//Create two shapes
var greenSquare: UIView?
var redSquare: UIView?
var animator: UIDynamicAnimator?

override func viewDidLoad() {
    super.viewDidLoad()

}


@IBAction func startbutton(sender: UIButton) {

    //Create the shapes
    var dimen = CGRectMake(25, 25, 60, 60)
    greenSquare = UIView(frame: dimen)
    greenSquare?.backgroundColor = UIColor.greenColor()

    dimen = CGRectMake(130, 25, 90, 90)
    redSquare = UIView(frame: dimen)
    redSquare?.backgroundColor = UIColor.redColor()

    //Add them to the screen
    self.view.addSubview(greenSquare!)
    self.view.addSubview(redSquare!)

    //Initialize the animator
    animator = UIDynamicAnimator(referenceView: self.view)

    //Gravity
    let gravity = UIGravityBehavior(items: [greenSquare!, redSquare!] )
    let direction = CGVectorMake(0.0, 1.0)
    gravity.gravityDirection = direction

    //Collision
    let boundries = UICollisionBehavior(items: [greenSquare!, redSquare!] )
    boundries.translatesReferenceBoundsIntoBoundary = true

    //Elasticity
    let bounce = UIDynamicItemBehavior(items: [greenSquare!, redSquare!] )
    bounce.elasticity = 0.5

    animator?.addBehavior(bounce)
    animator?.addBehavior(boundries)
    animator?.addBehavior(gravity)
    }

}

关于ios - 快速碰撞边界不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422665/

相关文章:

ios - 只使用 CLImageEditor 中的一个工具

iOS 状态栏背景与 UIAlertController 重叠

ios - 从 FBSDKGraphRequest 到 Storyboard

ios - 使用 UICollisionBehavior 调整 UIView 的大小

ios - 核心数据 : suggestions for model design

ios - objective-c : loop efficiently through particular range of elements within NSArray

Swift:转换集合,并创建自定义可转换协议(protocol)

ios - UITableView 上的 UIKit 动态

ios - 使 block 与屏幕边界碰撞

ios - 代码/iOS : How to enable App Groups without Developer Account?