如何重复动画(使用UIViewPropertyAnimator)一定次数? [英] How can I repeat animation (using UIViewPropertyAnimator) certain number of times?

查看:420
本文介绍了如何重复动画(使用UIViewPropertyAnimator)一定次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现按钮的脉动效果,因此需要重复几次弹簧效果,问题是我找不到有关提供哪些参数以及如何执行操作的任何信息

I want to achieve pulsation effect for the button, and hence need to repeat the spring effect several number of times, the issue is that I can't find any information about what parameters to provide and how to do it

let btnView = sayWordBtn.viewWithTag(0)
    btnView.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
    let mass: CGFloat = 2.0 // weight of the object
        let stiffness: CGFloat = 25.0 //elasticity
        let damping: CGFloat = 2*sqrt(mass*stiffness) // point where the system comes to rest in the shortest period of time
        let underDamping: CGFloat = damping * 0.5
        let initialVelocity: CGVector = CGVector.zero
        let springParameters: UISpringTimingParameters = UISpringTimingParameters(mass: mass, stiffness: stiffness, damping: underDamping, initialVelocity: initialVelocity)
        let animationDelay = 3

        let pulseEffect = UIViewPropertyAnimator(duration: 5, timingParameters: springParameters)
        pulseEffect.addAnimations( {[weak self] in
          btnView!.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
          })
        pulseEffect.isReversed = true
        pulseEffect.startAnimation(afterDelay: TimeInterval(animationDelay))


推荐答案

iOS 10 引入了一个名为 UIViewPropertyAnimator
您可以使用 UIView.animation 做相同的事情,但是为了获得更复杂的动画,苹果提供了 CoreAnimation 框架。

iOS 10 introduces a new object called UIViewPropertyAnimator. You can do the same things you did with UIView.animation but for achieving more complex animations, apple provides us CoreAnimation framework.

因为 UIViewPropertyAnimator 是一个编写良好且可高度扩展的API。它涵盖了许多与旧样式 UIView 动画相同的功能,但是为您提供了对动画的精细编程控制。这意味着您可以暂停进行中的动画,如果需要的话可以在以后的某个日期重新启动它,甚至可以动态修改可设置动画的属性(例如,将动画的端点更改为屏幕的右上角,而之前是其底部,剩下)。
我们创建它的一个实例并创建一个动画,如下所示:

Because UIViewPropertyAnimator is a well-written and highly-extensible API. It covers a lot of the same functionality as the ‘old-style’ UIView animations, but gives you fine-grained programmatic control over the animation. This means you can pause an in-progress animation, restart it at a later date if you wish and even dynamically modify the animatable properties (such as changing the animation’s end point to the top-right of the screen when it was previously the bottom-left). We create an instance of it and create an animation as follows:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.alpha = 0.35 
view.backgroundColor = .red
self.view.addSubview(view)
let newFrame = CGRect(x: 0, y: 0, width: 50, height: 50)
let viewFrameAnimator = UIViewPropertyAnimator(duration: 1.0, 
                                                  curve: .linear, 
                                             animations: { view.frame = newFrame })

现在我们可以使用以下方式与动画进行交互:

Now we can interacting with the animations using:

viewFrameAnimator.startAnimation..
viewFrameAnimator.stopAnimation..
viewFrameAnimator.finishAnimation(at:..

继续上面的示例:

viewFrameAnimator.startAnimation()

然后,返回到 alpha 等于 1 ,如果我们需要更多的动画选项,我们还可以启动 UIViewPropertyAnimator 打开类:

Then, to return to alpha equal to 1, if we need more animations options, we can also launch this UIViewPropertyAnimator open class:

let viewAlphaAnimator = UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0, 
                                                       delay: 0.0,
                                                     options: [.curveLinear],
                                                  animations: { view.alpha = 1.0 })

让我们添加更多的动画选项。现在,您不仅了解这个新对象。

let give us to add more animation options. Now you know more than this new object.

所以让我们开始解释如何重复动画,例如,如果我们想要重复 3 view.alpha = 1.0 (您可以看到每个重复输出的小闪光灯。)您应该

So let's start to explain how to repeat the animation, for example if we want to repeat the view.alpha = 1.0 for 3 times (you can see the little flash light for each repeation to output..) you should follow:

let viewAlphaAnimator = UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0,
                                                delay: 0.0,
                                                options: [.curveLinear,.repeat],
                                                animations: { UIView.setAnimationRepeatCount(3);view.alpha = 1.0 })

我希望这可以为您提供帮助。

I hope this can help you.

这篇关于如何重复动画(使用UIViewPropertyAnimator)一定次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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