我有一個在InterfaceBuilder中配置好的ViewController,用它的所有子視圖使用AutoLayout。佈局工作正常。當Autolay在玩時用UIDynamicAnimator進行動畫製作
我想使用UIDynamicAnimator提供的很酷的重力效果使其中一個子視圖反彈。我不認爲這將與AutoLayout一起工作,但我嘗試了它,它確實如此。它工作得很好!
我的問題是,這是預期的行爲?是否會在某個地方引起問題? Apple是否提供關於AutoLayout和UIDynamicAnimators相結合的指導?
下面是代碼,有興趣的人士:
var boundaryFrame = self.buttonBackgroundView.frame
boundaryFrame = CGRect(x: 0,
y: boundaryFrame.origin.y + (boundaryFrame.height + 1),
width: self.view.frame.width,
height: 2)
self.animator = UIDynamicAnimator(referenceView: self.view)
var gravity = UIGravityBehavior(items: [self.buttonBackgroundView])
gravity.magnitude = 0.5
var collision = UICollisionBehavior(items: [self.buttonBackgroundView])
var boundaryId: NSMutableString = NSMutableString(string: "bottomBoundary")
let boundaryPath = UIBezierPath(rect: boundaryFrame)
collision.addBoundaryWithIdentifier(boundaryId, forPath: boundaryPath)
let bounceProperties = UIDynamicItemBehavior(items: [self.buttonBackgroundView])
bounceProperties.elasticity = 0.5
// Move it up before causing it to bounce down to its original location
self.setMeUpTopConstraint.constant = 10
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
// Start animating
animator.addBehavior(gravity)
animator.addBehavior(collision)
animator.addBehavior(bounceProperties)
謝謝,馬特。在這種情況下,我將動畫返回到其最終佈局,並且在動畫過程中沒有其他佈局更改發生,因此在這種情況下使用也許可以。 3個問題給你:1.你是否說如果在動畫過程中發生佈局,那會導致問題? 2.之前從未聽說過「延遲的表現」,但Google向我展示了這是一件事情。你能指出我的任何資源嗎? 3.如果您想在使用自動佈局的視圖控制器中設置動畫(例如,彈跳或擺動)UI元素,您會怎麼做? – stone 2015-02-12 04:37:06
@skypecakes 1.是 - 或之後發生。但是,如果您的動畫將所有內容都返回到原來的位置,您將不會看到問題,因爲您所做的(使用框架)以及自動佈局將會執行的操作(使用約束)具有相同的結果。 2.見我的書:http://www.apeth.com/iOSBook/ch11.html#_delayed_performance 3.大主題,但看到我的文章在這裏:http://stackoverflow.com/questions/12943107/how-do- i-adjust-the-anchor-point-of-a -layer-when-auto-layout-is-being-used/14105757#14105757 – matt 2015-02-12 17:38:18
再次感謝。我現在正在閱讀你對延遲表現的討論。關於#1和#3,我必須承認,我對iOS中的動畫知之甚少,無法理解你的論文,甚至不知道我是如何在動畫中使用幀的!我喜歡深度潛水,花費足夠的時間來理解它。你如何學習如此多的動畫?有任何建議的人開始? – stone 2015-02-12 23:57:56