我想使用嵌入 UICollectionView 标题单元格中的自定义 UITextfield 来搜索 UICollectionView。我有一个委托方法,将输入的文本传递到主视图控制器,但是,您在文本字段中输入的每个字母都会导致键盘退出,因为集合视图已重新加载。
使用 Swift 输入嵌入 UICollectionView 标题单元格中的 UITextField 时如何防止键盘关闭?
// SearchHeaderView
protocol SearchHeaderViewDelegate: AnyObject {
func search(with searchString: String)
}
class SearchHeaderView: UICollectionViewCell, UITextFieldDelegate {
weak var delegate: SearchHeaderViewDelegate?
lazy var searchContainerView: InputContainerView = {
return InputContainerView(image: UIImage(named: "96_search_line")!,
textField: searchTextField)
}()
let searchTextField = CustomTextField(placeholder: "Search",
keyboard: .default,
autoCapitalization: .words)
override init(frame: CGRect) {
super.init(frame: .zero)
searchTextField.delegate = self
}
func textFieldDidChangeSelection(_ textField: UITextField) {
if (searchTextField.text?.count)! != 0 {
delegate?.search(with: textField.text!)
}
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个每3秒调用一次的函数.如何为左右摇晃的按钮制作摇动动画.
func shakeButton() {
if opened == false {
//Shake Animation
}
}
Run Code Online (Sandbox Code Playgroud) 在我的Xcode项目中,我想使用Swift更改/设置标签的背景颜色.我怎么能这么做呢?
我有这个,但它只适用于按钮.
label.backgroundColor = UIColor(red: 0/255, green: 159/255, blue: 184/255, alpha: 1.0)
Run Code Online (Sandbox Code Playgroud) 我有一个UIImageView,我想它,所以当我调用一个函数时,图像从平面(正常状态),到倾斜到右旋转(比如可能是20度旋转),然后回到它的平面(正常)状态.
这就是我现在所拥有的,但我无法获得理想的结果.
extension UIView {
func rotate(duration: CFTimeInterval = 2) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(20)
rotateAnimation.isRemovedOnCompletion = true
rotateAnimation.duration = duration
rotateAnimation.repeatCount=Float.infinity
self.layer.add(rotateAnimation, forKey: nil)
}
}
UIImageView.rotate()
Run Code Online (Sandbox Code Playgroud)