Lion 的答案确实有效,但并不流畅,因此当您选择另一行时,它对用户不友好(我认为它适用于颜色,但对于不同的字体,它不是最佳解决方案)。所以我决定仿射变换。这是代码:
// MARK: - Picker View Help
func pickerViewHelp_afterRowWasSelected(row: Int) {
let accountSelected = ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row]
// save ID of this currency account in NSDefaults
DefaultsManager.sharedInstance.getDefaultsForSharingBetweenExtensions().setValue(accountSelected.id, forKey: kNSDefaults_mainCurrencyAccountID)
DefaultsManager.sharedInstance.getDefaultsForSharingBetweenExtensions().synchronize()
// reload scales
delay(0) { [weak self] in
self?.pickerViewHelp_updateFontsForAllRows(self!.pickerViewAccounts, selectedIndex: row)
}
}
func pickerViewHelp_updateFontsForAllRows(pickerView: UIPickerView, selectedIndex: Int) {
if pickerViewHelp_rowWasChangedBeforeReloadingPicker { // we changed the row
// remove prev animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
pickerLabel?.layer.removeAllAnimations()
}
// start new animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
UIView.animateWithDuration(0.3) { [weak self] in
let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
// let pickerLabel = pickerView.viewForRow(i, forComponent: 0) as? UILabel
let pickerLabel = self?.pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
var transform = CATransform3DIdentity
transform = CATransform3DScale(transform, scale, scale, 1.01)
pickerLabel?.layer.transform = transform
}
}
}
else if pickerViewHelp_previouslySelectedRow == -1 { // first setup of the picker or we selected the same row
// remove prev animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
pickerLabel?.layer.removeAllAnimations()
}
// without animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
var transform = CATransform3DIdentity
transform = CATransform3DScale(transform, scale, scale, 1.01)
pickerLabel?.layer.transform = transform
}
}
else { // repeat 1 if (for now I want leave in like this, because if smth is wrong, I will need to change it separately
// remove prev animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
let pickerLabel = pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
pickerLabel?.layer.removeAllAnimations()
}
// start new animation
for i in 0..<pickerView.numberOfRowsInComponent(0) {
UIView.animateWithDuration(0.3) { [weak self] in
let scale: CGFloat = (i == selectedIndex) ? 1 : 0.5
// let pickerLabel = pickerView.viewForRow(i, forComponent: 0) as? UILabel
let pickerLabel = self?.pickerViewHelp_dictOfViewsInPicker[i] as? UILabel
var transform = CATransform3DIdentity
transform = CATransform3DScale(transform, scale, scale, 1.01)
pickerLabel?.layer.transform = transform
}
}
}
}
// MARK: - Picker view
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if ApiManager.sharedInstance.userService_currentUser?.arrayCurrencyAccounts == nil { // no account
return 0
}
else {
let count = ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts.count
return count
}
}
var pickerViewHelp_rowWasChangedBeforeReloadingPicker = false
var pickerViewHelp_dictOfViewsInPicker = [Int: UIView]()
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
var pickerLabel = view as? UILabel;
if (pickerLabel == nil)
{
pickerLabel = UILabel()
pickerLabel?.textAlignment = NSTextAlignment.Center
}
pickerLabel?.attributedText = help_getAttributedStringForWalletByAmount(ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].balance, currency: EnumCurrency(rawValue: ApiManager.sharedInstance.userService_currentUser!.arrayCurrencyAccounts[row].currency), scaleOfText: 1)
pickerViewHelp_dictOfViewsInPicker[row] = pickerLabel
return pickerLabel!;
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 60
}
var pickerViewHelp_previouslySelectedRow = -1
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerViewHelp_previouslySelectedRow != row {
pickerViewHelp_rowWasChangedBeforeReloadingPicker = true
}
else {
pickerViewHelp_rowWasChangedBeforeReloadingPicker = false
}
pickerViewHelp_previouslySelectedRow = row
pickerView.selectRow(row, inComponent: 0, animated: false) // because otherwise sometimes it stops between cells in the picker
if pickerViewHelp_rowWasChangedBeforeReloadingPicker {
// print("reload all components")
pickerView.reloadAllComponents()
}
pickerViewHelp_afterRowWasSelected(row)
}
它也不是很理想,但我们的设计师喜欢它,所以我们决定使用这种方式