所以我有這些文本框,我才意識到它們都具有相同的屬性,所以我創建了一個名爲「UserInputs」,並從UITextField
擴展新的類,一切工作正常,除了一兩件事,UITextFieldDelegate
功能沒有按」 t的工作,我的意思是當我專注於他們這是行不通的,我想在代碼中添加它,因爲當你把注意力集中在我的輸入字段上時,他們會改變邊界,我如何恰當地從子類別UITextField
正確子類UITextField,用於迅速
唯一的問題我有那個功能:
textFieldDidBeginEditing
textFieldDidEndEditing
等都不起作用。
這是我培訓班裏發生的一切:
import Foundation
import UIKit
class RegistrationViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var firstName: UserInputs!
@IBOutlet weak var test: UserInputs!
override func viewDidLoad() {
super.viewDidLoad()
self.firstName.delegate = self
self.test.delegate = self
}
}
這是我子:
class UserInputs: UITextField{
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
createBorder()
}
func createBorder(){
let border = CALayer()
let width = CGFloat(2.0)
border.borderColor = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).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
//print("border created")
}
func textFieldDidBeginEditing() {
print("focused")
self.pulseBorderColor()
}
func textFieldDidEndEditing() {
print("lost focus")
self.reversePulseBorderColor()
}
func pulseBorderColor(){
let pulseAnimation = CABasicAnimation(keyPath: "borderColor")
pulseAnimation.duration = 0.35
pulseAnimation.fromValue = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
pulseAnimation.toValue = UIColor(red: 252/255, green: 180/255, blue: 29/255, alpha: 1.0).CGColor
pulseAnimation.fillMode = kCAFillModeForwards
pulseAnimation.removedOnCompletion = false
pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.layer.sublayers![0].addAnimation(pulseAnimation,forKey: nil)
}
func reversePulseBorderColor(){
let pulseAnimation = CABasicAnimation(keyPath: "borderColor")
pulseAnimation.duration = 0.35
pulseAnimation.fromValue = UIColor(red: 252/255, green: 180/255, blue: 29/255, alpha: 1.0).CGColor
pulseAnimation.toValue = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).CGColor
pulseAnimation.fillMode = kCAFillModeForwards
pulseAnimation.removedOnCompletion = false
pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.layer.sublayers![0].addAnimation(pulseAnimation,forKey: nil)
}
}
當我沒有子類,並在做這裏面我主要的代碼工作類,但創建子類聚焦功能後停止工作,其他所有工作
主要問題是,我想要實現
func textFieldDidBeginEditing() {
print("focused")
}
func textFieldDidEndEditing() {
print("lost focus")
}
這些在我的文本框,所以我不過來寫,一遍又一遍
也許,但它並沒有解決問題,當我寫的代碼有上沒有建議:textFieldDid ......但是當我在主類寫它給了我建議 – nikagar4
這些委託方法屬於在委託而不是在UserInputs類中。 –
它仍然無法正常工作,我將發佈主要的課程,以便您可以完全看到我在做什麼 – nikagar4