简体   繁体   English

UIGravityBehavior在移动项目后不起作用

[英]UIGravityBehavior do not work after move item

When I start my app I see that UILabel falls down, then I move it to up and it stay there. 当我启动我的应用程序时,我看到UILabel掉落了,然后将其向上移动并停留在那里。 Why does it not fall? 为什么不跌倒? How make GravityBehavior after move element? 移动元素后如何制作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
            }
        }
    }

Everything you did is right only thing you have to change is once the pangesture state is ended you have to add gravity behaviour again.Code i have modified is given below 您所做的一切都是正确的,唯一需要更改的就是手势状态结束后,您必须再次添加重力行为。

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

The above code will fullfill your expectations. 上面的代码将满足您的期望。 Best practice is to remove previous behaviour from animator animator.removeAllBehaviors() before adding the new one.I hope this may help you. 最佳做法是在添加新动画之前从动画师animator.removeAllBehaviors()中删除以前的行为。希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UIGravityBehavior更改为视图中的特定点 - Change UIGravityBehavior to a certain point in a view UICollisionBehavior和UIGravityBehavior无法与DispatchQueue一起正常工作 - UICollisionBehavior and UIGravityBehavior not working as expected with DispatchQueue 多种类型的segue仍然无法移动视图控制器 - Multiple types of segues still do not work to move view controller 将项目移至部分 - Move item to section UICollectionview - 移动项目时闪烁 - UICollectionview - blink when move item 下载后移动文件 - Move File After Downloading tableView.setContentOffset(_, animation:) 在 beginUpdates() 后不起作用 - tableView.setContentOffset(_, animated:) do not work after beginUpdates() 调用 URLSession 后我不能做其他工作 - I can not do other work after calling URLSession 达到字符数后,如何以编程方式将 cursor 从一个 NSTextField 移动到另一个? - How do I programatically move the cursor from one NSTextField to another after a character count has been reached? 为什么我的某些锚约束不起作用,而其他锚约束却对同一个项目起作用? - Why dont some of my anchor constraints work while others do for the same item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM