【发布时间】:2018-04-11 13:39:50
【问题描述】:
我想用动画改变 UIPickerView 中选定行的背景颜色。我在选择新行时重新加载所有组件,并且在 viewForRow 函数中如果选择了当前行,我将其背景颜色设置为红色。但是,它看起来像在错误中。第一个屏幕截图是我想要实现的,第二个屏幕截图是在我的应用程序中。顺便说一句,如果我能用动画设置红色就太好了
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.reloadAllComponents()
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: width, height: height)
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: height, height: height)
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 30)
label.text = numbers[row]
view.addSubview(label)
view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
if pickerView.selectedRow(inComponent: component) == row {
label.attributedText = NSAttributedString(string: numbers[row], attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 30),NSAttributedStringKey.foregroundColor:UIColor.white])
view.backgroundColor = UIColor.red
}
return view
}
【问题讨论】:
标签: ios swift uiview uipickerview