在播放自动布局时使用UIDynamicAnimator进行动画处理 [英] Animating with UIDynamicAnimator when Autolayout is in play

查看:167
本文介绍了在播放自动布局时使用UIDynamicAnimator进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewController,它已在InterfaceBuilder中配置为对其所有子视图使用AutoLayout.布局工作正常.

I have a ViewController that has been configured in InterfaceBuilder to use AutoLayout for all of its subviews. Layout is working fine.

我想使用UIDynamicAnimator提供的酷重力效果使子视图之一反弹.我认为这不适用于AutoLayout,但我尝试了,但确实如此.效果很好!

I want to make one of the subviews bounce using the cool gravity effects provided by UIDynamicAnimator. I didn't think that would work with AutoLayout, but I tried it and it does. It works quite well!

我的问题是,这是预期的行为吗?它会在某处引起问题吗? Apple是否提供有关结合使用AutoLayout和UIDynamicAnimators的任何指导?

My question is, is this the expected behavior? Is it going to cause problems somewhere? Does Apple provide any guidance around combining AutoLayout and UIDynamicAnimators?

这是代码,适合任何有兴趣的人:

Here is the code, for anyone interested:

    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)

推荐答案

我的问题是,这是预期的行为吗?它会在某处引起问题吗? Apple是否提供有关结合使用AutoLayout和UIDynamicAnimators的任何指导?

My question is, is this the expected behavior? Is it going to cause problems somewhere? Does Apple provide any guidance around combining AutoLayout and UIDynamicAnimators?

基本上,UIKit Dynamics在自动布局中不能很好地发挥作用.基本上,规则是更改某事物的框架/中心与自动布局相反,而这正是UIKit Dynamics所做的.您没有遇到问题的唯一原因是,偶然地,您正在制作动画的视图上没有发生布局.

Basically, UIKit Dynamics does not play well with auto layout. Basically the rule is that changing the frame / center of something is contrary to auto layout - and that is exactly what UIKit Dynamics does. The only reason you are not encountering a problem is that, by chance, layout does not happen to be occurring on the view(s) you are animating.

测试方法是这样.运行您的代码,然后(动画完成后;可能需要使用延迟的性能)运行

The way to test is this. Run your code, and then (when the animation has finished; possibly you'll need to be using delayed performance) run

self.view.layoutIfNeeded

这导致布局发生.如果那时候事情跳来跳去,那就暴露出了问题所在.

That causes layout to happen. If things jump around at that moment, it exposes the issue.

这篇关于在播放自动布局时使用UIDynamicAnimator进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆