アプリを起動すると、UILabel が落ちて、上に移動してそこにとどまることがわかります。落ちないのはなぜ?要素を移動した後に GravityBehavior を作成するにはどうすればよいですか?
class ViewController: UIViewController
{
var helloWorldLabel: UILabel!
var panGestureRecognizer: UIPanGestureRecognizer!
var animator: UIDynamicAnimator!
override func viewDidLoad()
{
super.viewDidLoad()
var str:NSString = "Hello World"
let labelFrame = CGRect(x: 0, y: 0, width:200, height: 100);
helloWorldLabel = UILabel(frame: labelFrame)
helloWorldLabel.userInteractionEnabled = true
helloWorldLabel.text = str as String
helloWorldLabel.frame = labelFrame;
helloWorldLabel.backgroundColor = UIColor.grayColor()
helloWorldLabel.center = view.center
view.addSubview(helloWorldLabel)
panGestureRecognizer = UIPanGestureRecognizer(target: self,action: "handlePanGestures:")
helloWorldLabel.addGestureRecognizer(panGestureRecognizer)
animator = UIDynamicAnimator(referenceView: view)
let collisionBehavior = UICollisionBehavior(items: [helloWorldLabel])
collisionBehavior.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collisionBehavior)
let gravityBehavior = UIGravityBehavior(items: [helloWorldLabel])
animator.addBehavior(gravityBehavior)
}
func handlePanGestures(sender: UIPanGestureRecognizer)
{
if sender.state != .Ended && sender.state != .Failed
{
let location = sender.locationInView(sender.view!.superview!)
sender.view!.center = location
}
}
}