快速脉冲动画

时间:2016-01-13 07:45:44

标签: ios xcode swift animation uitextfield

我试图制作一个脉冲动画,我发现了这个: ios - how to do a native "Pulse effect" animation on a UIButton 它很有用,但我需要别的东西, 所以我有textfield,UITextField有边框, 当你专注于它我希望边框改变颜色,但具体方式 我希望它从中心开始然后传播,我找不到动画所以我会尝试用文字来演示它:B =黑色 Y =黄色。 这是边界:

BBBBBBB

边界变得集中:

BBBYBBB

BBYYYBB

BYYYYYB

YYYYYYY

动画结束了, 焦点丢失:

BYYYYYB

BBYYYBB

BBBYBBB

BBBBBBB

我的代码看起来像这样:

let pulseAnimation = CABasicAnimation(keyPath: "borderColor")
    pulseAnimation.duration = 3
    pulseAnimation.fromValue = UIColor.darkGrayColor().CGColor
    pulseAnimation.toValue = UIColor.yellowColor().CGColor
    pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEasInEaseOut)
    pulseAnimation.autoreverses = true
    //pulseAnimation.repeatCount = FLT_MAX
    x.layer.sublayers![0].addAnimation(pulseAnimation, forKey: nil)

它有效,但我需要它做其他事情

1 个答案:

答案 0 :(得分:9)

你去吧

let pulseAnimation = CABasicAnimation(keyPath: "opacity")
pulseAnimation.duration = 30
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = Float.greatestFiniteMagnitude
self.view.layer.addAnimation(pulseAnimation, forKey: nil)