Подтвердить что ты не робот

Как сделать нативную анимацию "Импульсный эффект" на UIButton - iOS

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

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

4b9b3361

Ответ 1

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

стриж

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")

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

Ответ 2

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

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)

Ответ 3

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

pulseAnimation.fromValue = NSNumber(float: 0.0)

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

self.view.layer.addAnimation(pulseAnimation, forKey: "layerAnimation")

Ответ 4

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)
}