[ios] UIButton에서 네이티브 “펄스 효과”애니메이션을 수행하는 방법-iOS

UIButton에 일종의 펄스 애니메이션 (무한 루프 “스케일 인-스케일 아웃”)을 사용하여 사용자의주의를 즉시 끌고 싶습니다.

-webkit-animation-바깥 쪽 링을 사용하여 펄스 효과를 만드는 방법 이 링크를 보았지만 네이티브 프레임 워크 만 사용하여이 작업을 수행 할 수있는 방법이 있는지 궁금합니다.



답변

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

“레이어 콘텐츠 애니메이션”기사 참조


답변

여기에 대한 빠른 코드가 있습니다.)

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)


답변

빠른 코드에는 누락 fromValue되어 작동하도록 추가해야했습니다.

pulseAnimation.fromValue = NSNumber(float: 0.0)

또한 forKey설정해야합니다 removeAnimation. 그렇지 않으면 작동하지 않습니다.

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


답변

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


답변