gpt4 book ai didi

swift - UIGravityBehavior 在移动项目后不起作用

转载 作者:行者123 更新时间:2023-11-30 13:41:29 25 4
gpt4 key购买 nike

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

最佳答案

你所做的一切都是正确的,唯一需要改变的是一旦手势状态结束,你必须再次添加重力行为。下面给出了我修改的代码

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() 中删除以前的行为。我希望这可以帮助您。

关于swift - UIGravityBehavior 在移动项目后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com