Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 276
nikagar4 Avatar asked Dec 05 '22 01:12

nikagar4


1 Answers

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)
like image 69
BilalReffas Avatar answered Dec 06 '22 15:12

BilalReffas