Как сделать нативную анимацию «Импульсный эффект» на UIButton - iOS - PullRequest
85 голосов
/ 10 ноября 2011

Мне бы хотелось иметь какую-то импульсную анимацию (бесконечный цикл «масштабирование в масштабе») на кнопке UIB, чтобы она сразу привлекала внимание пользователей.

Я видел эту ссылку Как создать импульсный эффект с помощью -webkit-animation - outward rings но мне было интересно, есть ли способ сделать это только с использованием нативного фреймворка?

Ответы [ 4 ]

191 голосов
/ 10 ноября 2011
CABasicAnimation *theAnimation;

theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=1.0;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.0];
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"]; //myButton.layer instead of

Swift

let pulseAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
pulseAnimation.duration = 1
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
view.layer.add(pulseAnimation, forKey: "animateOpacity")

См. Статью «Анимация содержимого слоя»

26 голосов
/ 08 декабря 2014

Вот быстрый код для него;)

let pulseAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
pulseAnimation.duration = 1.0
pulseAnimation.toValue = NSNumber(value: 1.0)
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
self.view.layer.add(pulseAnimation, forKey: nil)
8 голосов
/ 05 августа 2015

В быстром коде отсутствует fromValue, мне пришлось добавить его, чтобы он заработал.

pulseAnimation.fromValue = NSNumber(float: 0.0)

Также следует установить forKey, иначе removeAnimation не работает.

self.view.layer.addAnimation(pulseAnimation, forKey: "layerAnimation")
3 голосов
/ 09 июня 2016
func animationScaleEffect(view:UIView,animationTime:Float)
{
    UIView.animateWithDuration(NSTimeInterval(animationTime), animations: {

        view.transform = CGAffineTransformMakeScale(0.6, 0.6)

        },completion:{completion in
            UIView.animateWithDuration(NSTimeInterval(animationTime), animations: { () -> Void in

                view.transform = CGAffineTransformMakeScale(1, 1)
            })
    })

}


@IBOutlet weak var perform: UIButton!

@IBAction func prefo(sender: AnyObject) {
    self.animationScaleEffect(perform, animationTime: 0.7)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...