0

当我启动我的应用程序时,我看到 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
            }
        }
    }
4

1 回答 1

0

您所做的一切都是正确的,唯一需要更改的是,一旦 pangesture 状态结束,您必须再次添加重力行为。我修改过的代码如下所示

class JsonParser: UIViewController {

var animator: UIDynamicAnimator!
var helloWorldLabel: UILabel!
var panGestureRecognizer: UIPanGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
    let 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)
    AddCollisionAndGravity()

func AddCollisionAndGravity () {
    animator.removeAllBehaviors()

    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
    }else if sender.state == UIGestureRecognizerState.Ended{

       AddCollisionAndGravity()
    }
}

上面的代码将满足您的期望。最佳做法是在添加新行为之前从动画师animator.removeAllBehaviors()中删除以前的行为。我希望这对您有所帮助。

于 2016-05-12T10:16:12.770 回答