在UIDynamics Swift中检测到运动抖动和碰撞后更改图像项

时间:2016-03-03 17:07:34

标签: ios swift collision motion uikit-dynamics

我有以下代码,我想显示一个警报并在碰撞后更改图像并基于生成的随机数。 但是,它显示了碰撞前的结果...... 任何想法???

import UIKit
class ViewController: UIViewController,UICollisionBehaviorDelegate {    
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var shakelabel: UILabel!

var animator: UIDynamicAnimator!
var gravity: UIGravityBehavior!
var collision: UICollisionBehavior!

// Random number generation
let Bump = Int(arc4random_uniform(UInt32(2)))

override func motionEnded(motion: UIEventSubtype,
    withEvent event: UIEvent?) {

      if motion == .MotionShake {
            animator = UIDynamicAnimator(referenceView: view)
            gravity = UIGravityBehavior(items: [imageView])
            animator.addBehavior(gravity)
            collision = UICollisionBehavior(items: [imageView])
            collision.translatesReferenceBoundsIntoBoundary = true
            animator.addBehavior(collision)
        }          
        if Bump == 0 {
            let controller = UIAlertController(title: "W O W",
            message: "YOU WON",
            preferredStyle: .Alert)
            controller.addAction(UIAlertAction(title: "OK",
            style: .Default,
            handler: nil))
            presentViewController(controller, animated: true, completion: nil)             
        }
        if Bump == 1 {
            imageView.image = UIImage(named: "image_loose")               
            let controller = UIAlertController(title: "O O P S",
            message: "YOU LOOSE",
            preferredStyle: .Alert)
            controller.addAction(UIAlertAction(title: "OK",
            style: .Default,
            handler: nil))
            presentViewController(controller, animated: true, completion: nil)
        }
}
override func viewDidLoad() {
    super.viewDidLoad()
  }
}

0 个答案:

没有答案