Swift Animate Text Box

I want to create an animation for my UItextfield. To do this, I created a subclass of UITextField, and I saved 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 text box, I want to update the border of the text box and make it green. Is there an effective way to do this? I tried to assign a function whenever someone touches inside the text box in my main view controller, but it does not seem to work.

+4
source share
1 answer

. UIView "Touches Enabled", , " " uitext .

UIButton.

, , X Y.

"setNeedsDisplay" , .

UIView ?

. , UITextfield, , () , ( ):

, UITextView

:

 optional func textViewDidBeginEditing(_ textView: UITextView)

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618610-textviewdidbeginediting

:

 optional func textViewDidEndEditing(_ textView: UITextView)

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618628-textviewdidendediting

+2

Source: https://habr.com/ru/post/1679577/


All Articles