gpt4 book ai didi

ios - swift :我 super 困惑! UILabel 上的 UIGravityBehavior vs NSTimer vs CGPointMake

转载 作者:行者123 更新时间:2023-11-28 07:12:42 27 4
gpt4 key购买 nike

所以我声明了一个 UILabel!名为“textLabel”,具有上升的重力效果。现在,当它达到 -30 时,我希望标签重新出现在 (200, 444) 处。我使用 NSTimer 检查 if (labelText > -30) 但是当它到达/通过该点时,它只是在它应该出现的地方(在中间附近)闪烁,但它不是从中间开始的。这是我的大部分代码。

如何让标 checkout 现在它的新位置?所以它可以在达到 y 轴上的 -30 后再次循环?我找了又找。帮助PPPPPP

@IBOutlet weak var textLabel: UILabel!

var gravity: UIGravityBehavior!
var animator: UIDynamicAnimator!
var itemBehavior: UIDynamicItemBehavior!

var boundaryTimer = NSTimer()

Override func viewDidLoad() {
animator = UIDynamicAnimator(referenceView: view)
gravity = UIGravityBehavior(items: [textLabel])
animator.addBehavior(gravity)

boundaryTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "leftBoundary", userInfo: nil, repeats: true)

itemBehavior = UIDynamicItemBehavior(items: [textLabel])
itemBehavior.elasticity = 1.2
gravity.gravityDirection = CGVectorMake(0.0 , -0.01)
animator.addBehavior(itemBehavior)
}

func leftBoundary() {
if textLabel.center.y < 40 {

self.textLabel.center = CGPointMake(200, 444)
}
}

最佳答案

您可以添加然后删除附件行为,而不是设置中心,例如:

func leftBoundary() {
if textLabel.center.y < 40 {
let attachment = UIAttachmentBehavior(item: textLabel, attachedToAnchor: textLabel.center)
animator.addBehavior(attachment)
attachment.anchorPoint = view.center
attachment.action = {[unowned self, attachment] in
if self.textLabel.center.y > 100 {
self.animator.removeBehavior(attachment)
}
}
}
}

请注意,您可能希望更改 itemBehavior,使 allowRotationfalse(取决于您所需的 UI)。

另请注意,我在这里使用的这个action 也可以用于您的gravity 行为。因此,不要使用计时器(它可能不会总是同时捕获标签),而是使用 action,它会在动画的每一帧上调用。

关于ios - swift :我 super 困惑! UILabel 上的 UIGravityBehavior vs NSTimer vs CGPointMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27745672/

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