Search code examples
iosxcodeswiftanimationuitextfield

Swift Pulse Animation


i'm trying to make a pulse animation, i've found this: How to do a native "Pulse effect" animation on a UIButton - iOS and it was useful, but i need something else, so i have textfield, UITextField which has border, when you focus on it i want the border to change color, but in specific way

I want it to start from center and then spread, I could not find the animation so i will try to demonstrate it via text: B = Black, Y = Yellow.

this is the border:

BBBBBBB

border gets focused:

BBBYBBB
BBYYYBB
BYYYYYB
YYYYYYY

animation is over, focus lost:

BYYYYYB
BBYYYBB
BBBYBBB
BBBBBBB

my code looks something like this:

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)

it works but i need it to do something else


Solution

  • This animation continually pulses from 0 to 1 opacity over 3 seconds:

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