gpt4 book ai didi

Swift 动画文本框

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:46 25 4
gpt4 key购买 nike

我想为我的 UItextfield 创建一个动画。为此,我创建了 UITextField 的子类,并将我的代码存储在那里:

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)
}

}

但是,当有人在文本字段内单击时,我想更新文本字段的边框并将其设为绿色。有没有一种有效的方法来做到这一点?我试图在有人触摸我的主视图 Controller 中的文本框内部时为其分配一个函数,但它似乎不起作用。

最佳答案

我建议不要合并这些行为。相反,添加一个带有“已启用触摸”的 UIView,然后向该 View 添加触摸监听器,点击时将消息发送到自定义 uitextfield 中的便捷方法,或者直接更改它的外观。

这也适用于 UIButton。

出于布局目的,只需使按钮或 View 具有与标签相同的边缘约束及其中心 X 和 Y 位置。

不要忘记为您的自定义文本标签调用“setNeedsDisplay”以强制其重绘。

What is the most robust way to force a UIView to redraw?

编辑:如果您只对当前正在编辑 UITextfield 时更改边框颜色感兴趣,请向其添加一个委托(delegate),并在它成为第一响应者(正在编辑)时相应地更改一次外观,并在它退出时相应地更改一次外观第一响应者(停止编辑):

How to know when UITextView became first responder

在此处更改边框:

 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

关于Swift 动画文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44620940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com