Ios 在Swift中设置选定UIPickerView行的背景色动画
我想用动画更改UIPickerView中选定行的背景色。当选择新行时,我将重新加载所有组件。在viewForRow函数中,如果选择当前行,我将其“背景色”设置为红色。然而,它看起来像是在bug中。第一个屏幕截图是我想要实现的,第二个屏幕截图在我的应用程序中。顺便说一句,如果我可以设置动画的红色将是非常好的Ios 在Swift中设置选定UIPickerView行的背景色动画,ios,swift,uiview,uipickerview,Ios,Swift,Uiview,Uipickerview,我想用动画更改UIPickerView中选定行的背景色。当选择新行时,我将重新加载所有组件。在viewForRow函数中,如果选择当前行,我将其“背景色”设置为红色。然而,它看起来像是在bug中。第一个屏幕截图是我想要实现的,第二个屏幕截图在我的应用程序中。顺便说一句,如果我可以设置动画的红色将是非常好的 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
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
}
一种稍有不同的方法,因为我找不到一种平滑的方法来为选定行上的背景设置动画。这是可以做到的,但没有太大的改进,因此尝试一下:
现在,您的代表可以简单地:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//no need to reload
//do whatever else you want
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
/*
Just return a `UILabel`. No need to put it in a `UIView`.
Nothing special either, just slap text into it
*/
var label = view as? UILabel
if label == nil {
label = UILabel()
//All the rest are safe force unwraps so chill tf out
label!.frame = CGRect(x: 0, y: 0, width: height, height: height)
label!.textAlignment = .center
label!.font = UIFont.systemFont(ofSize: 30)
label!.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
}
label!.text = arrDatasource[row]
return label!
}
我明白了。但在这个解决方案中,我想我不能将背景色设置为pickerview?它覆盖了视图,对吗?@EmreÖnder:)但是如果你有一个特定的设计,请告诉我,我会看看我是否能给你一些东西。我正在尝试对这个设计进行详细编码;我找到了一个图书馆;我想我可以用这个图书馆来完成它。当然,如果我能学会如何:)@EmreÖ在看
AZAPicker
的代码时,它是一个UIScrollView
和UIStackView
,中间有一个遮罩视图。但是,是的,根据您的设计,它是平的,不像默认的UIPickerView
那样弯曲。随它去吧。分叉并根据您的需要修改遮罩。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//no need to reload
//do whatever else you want
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
/*
Just return a `UILabel`. No need to put it in a `UIView`.
Nothing special either, just slap text into it
*/
var label = view as? UILabel
if label == nil {
label = UILabel()
//All the rest are safe force unwraps so chill tf out
label!.frame = CGRect(x: 0, y: 0, width: height, height: height)
label!.textAlignment = .center
label!.font = UIFont.systemFont(ofSize: 30)
label!.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
}
label!.text = arrDatasource[row]
return label!
}