使用UIDynamicAnimator水平动画UIView [英] Animate UIView horizontally using UIDynamicAnimator

查看:91
本文介绍了使用UIDynamicAnimator水平动画UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,我很尴尬地说我很困惑。

I have gone through the documentation, and I am embarrassed to say I am confused.

情景

我有一个 UIView ,就像3 UIButtons 的容器一样。这个容器最初的边界是 {0,0,35,35} ,里面的每个按钮都有相同的坐标( alpha 0)。在用户的特定操作上,容器将界限更改为 {0,0,100,35} ,并且该按钮被动画为x-origin 5,35,并且分别为65, alpha 1,这样它们就会在调整大小的容器中展开。我将此称为容器的展开状态。用户采取相同的操作,将其切换回原来的合同状态

I have a UIView, that acts like a container for 3 UIButtons. This container is initially of bounds {0, 0, 35, 35}, with each button inside with the same co-ordinates (with alpha 0). On a specific action by the user, the container changes bounds to {0, 0, 100, 35}, and the button are animated to x-origin 5, 35, and 65, respectively, with alpha 1, such that they are spread out inside the resized container. I refer to this as the expanded state of the container. The same action by the user, toggles it back to the original contracted state.

目标

我目前正在使用 [UIView animateWithDuration:] 块执行此操作,但是想使用 UIDynamicAnimator 添加一个弹性效果,这样,当切换到展开状态时,容器会在弹跳时调整大小(再调整一点,然后弹回到目标边界),按钮会弹回好吧(进一步移动,然后反弹回目标界限)。

I am currently doing this using the [UIView animateWithDuration:] block, but would like to use the UIDynamicAnimator to add a bouncy effect, such that, when toggling to expanded state , the container resizes with a bounce (resizes a bit extra, and bounces back to target bounds), and the buttons bounce as well (move a bit further, and bounce back to their target bounds).

困惑

UIDynamicAnimator UIDynamicBehavior UIAttachmentBehavior UIDynamicItem .....所有这些都导致我对 UIKitDynamic 的理解溢出。我想我应该使用 UISnapBehavior ,但我不知道如何去做。

UIDynamicAnimator, UIDynamicBehavior, UIAttachmentBehavior, UIDynamicItem.....all these are causing my understanding of UIKitDynamic to overflow. I think I should be using UISnapBehavior, but I do not know how to go about it.

推荐答案

事实证明,使用 UIDynamicAnimator ,并且不需要所有其他动态动画。我需要的只是 animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion: class method。只需使用它代替任何简单的 animateWithDuration:方法,它就会产生我想要实现的确切行为。

As it turns out, using UIDynamicAnimator, and all other dynamic animations was not needed. All I needed was the animateWithDuration:delay:usingSpringWithDamping: initialSpringVelocity: options: animations: completion: class method. Just use it in place of any simple animateWithDuration: method, and it yields the exact behaviour i intended to achieve.

示例代码:

    [UIView animateWithDuration:0.4
                          delay:0
         usingSpringWithDamping:0.5
          initialSpringVelocity:0.5
                        options:0
                     animations:^{

                         //Animation code

                     } completion:nil];

这篇关于使用UIDynamicAnimator水平动画UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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