I want to create an animation for my UItextfield. To do this, I created a sub class of UITextField and I stored my code there:
import Foundation
import UIKit
class textfieldEdit: UITextField, UITextFieldDelegate {
let border = CALayer()
let width = CGFloat(2.0)
required init?(coder aDecoder: (NSCoder!)) {
super.init(coder: aDecoder)
self.delegate=self;
border.borderColor = UIColor( red: 216/255, green: 216/255, blue: 216/255, alpha: 100 ).cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
override func draw(_ rect: CGRect) {
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
}
override func awakeFromNib() {
super.awakeFromNib()
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
}
}
However, when someone clicks inside the textfield, I want to update the border of the textfield and make it green. Is there an efficient way to do this? I tried to assign a function to whenever someone touches inside of the textbox in my main view controller but it doesnt seem to work.