ios 制作动画来扩展和缩小 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22395712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:44:29  来源:igfitidea点击:

Making an animation to expand and shrink an UIView

ioscocoa-touchcaanimation

提问by Flying_Banana

I want to create an animation that will resize an UIView and its contents by a factor. Basically, I want to make an animation that first expands the view then shrinks it back to the original size.

我想创建一个动画,该动画将按一个因子调整 UIView 及其内容的大小。基本上,我想制作一个动画,首先扩展视图然后将其缩小回原始大小。

What is the best way to do this? I tried CALayer.contentScale but it didn't do anything at all.

做这个的最好方式是什么?我尝试了 CALayer.contentScale 但它根本没有做任何事情。

回答by JJC

You can nest some animation blocks together like so:

您可以将一些动画块嵌套在一起,如下所示:

Objective-C:

目标-C:

[UIView animateWithDuration:1
                 animations:^{
                     yourView.transform = CGAffineTransformMakeScale(1.5, 1.5);
                 }
                 completion:^(BOOL finished) {
                     [UIView animateWithDuration:1
                                      animations:^{
                                          yourView.transform = CGAffineTransformIdentity;

                                      }];
                 }];

Swift 2:

斯威夫特 2:

UIView.animateWithDuration(1, animations: { () -> Void in
    yourView.transform = CGAffineTransformMakeScale(1.5, 1.5)
    }) { (finished: Bool) -> Void in
        UIView.animateWithDuration(1, animations: { () -> Void in
            yourView.transform = CGAffineTransformIdentity
        })}

Swift 3 & 4:

斯威夫特 3 和 4:

UIView.animate(withDuration: 1, animations: {
    yourView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}) { (finished) in
    UIView.animate(withDuration: 1, animations: { 
        yourView.transform = CGAffineTransform.identity
    })
}

and replacing the scale values and durations with your own.

并用您自己的比例值和持续时间替换。

回答by ecnepsnai

Here is a smaller approach that also loops:

这是一个较小的方法,它也循环:

[UIView animateWithDuration:1
                      delay:0
                    options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat
                 animations:^{
                     yourView.transform = CGAffineTransformMakeScale(1.5, 1.5);
                 }
                 completion:nil];

The option UIViewKeyframeAnimationOptionRepeatis what makes it loop, if you don't want it to keep "breathing". The animation block acts as the "inhale" and the UIViewKeyframeAnimationOptionAutoreverseoption automatically plays the "exhale" animation.

UIViewKeyframeAnimationOptionRepeat如果您不希望它保持“呼吸”,则该选项是使其循环的原因。动画块充当“吸气”,UIViewKeyframeAnimationOptionAutoreverse选项自动播放“呼气”动画。

回答by Stephen Collins

Swift 5 UIView extension:

Swift 5 UIView 扩展:

extension UIView {
    func pulse(withIntensity intensity: CGFloat, withDuration duration: Double, loop: Bool) {
        UIView.animate(withDuration: duration, delay: 0, options: [.repeat, .autoreverse], animations: {
            loop ? nil : UIView.setAnimationRepeatCount(1)
            self.transform = CGAffineTransform(scaleX: intensity, y: intensity)
        }) { (true) in
            self.transform = CGAffineTransform.identity
        }
    }
}

And then use the following:

然后使用以下内容:

yourView.pulse(withIntensity: 1.2, withDuration: 0.5, loop: true)

Just make sure you replace yourViewwith your own view.

只要确保yourView用自己的视图替换即可。