所以我宣布了一个UILabel!名为"textLabel“,它具有上升的重力效应。现在,当它达到-30时,我希望标签在(200,444)处重新出现.我使用一个NSTimer来检查是否(labelText > -30),但是当它到达/通过这个点时,它只会在它应该出现的地方闪烁(在中间),但它不是从中间开始的。这是我的大部分代码。
我如何使标签出现在它的新位置?所以,当它在y轴上达到-30时,它就可以再次循环了??我搜了又搜。HELPPPPPPP
@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)
}
}发布于 2015-01-02 17:21:56
与其设置center,您还可以添加并删除附件行为,例如:
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,以便allowRotation是false (取决于您想要的UI)。
还要注意,我在这里使用的这个action也可以用于gravity行为。因此,不要使用计时器(可能不总是同时捕获标签),而是使用action,它在动画的每一个帧上都被调用。
发布于 2015-01-02 17:21:44
您应该首先从动画师中删除所有标签行为。
这与UIKit动态的工作方式有关。它实际上只操作视图的层/表示层,因此,如果您想自己设置位置(通过视图,这也会影响该层),则需要重置所有内容。
此外,我认为与计时器的设置不是很有效。相反,您应该尝试一下KVO (键值观察)。您可以观察关键路径“cent.y”,并在其超过限制时作出反应。
https://stackoverflow.com/questions/27745672
复制相似问题