问题描述
我有一个自定义的UIPickerview,我不想使用datepicker。我想实现这样的功能:当用户向下/向上滚动小时时,AM / PM组件在小时滚动时切换。这意味着我需要在调用pickerView didSelectRow之前切换它。有没有办法做到这一点?
I have a customized UIPickerview and I do not want to use a datepicker. I want to implement the feature where when a user scrolls down/up the hours, the AM/PM component switches while the hour is scrolling. This means that I need to switch it before pickerView didSelectRow is called. Is there a way to do this?
谢谢
推荐答案
使用以下方法,
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// put your logic here.
}
上述方法来自UIPickerViewDelegate,如果用户使用pickerview选择任何元素,此方法自动触发。
Above method is from UIPickerViewDelegate, If user selects any element using pickerview, this method is automatically triggered.
希望对您有所帮助。
编辑:
我认为您应该使用以下方法进行检测 - 用户在哪个方向滚动?
I think you should use following method for detecting - in which direction user is scrolling ?
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"scrolling to row is %@",[NSString stringWithFormat:@"index_%i",row]);
return [NSString stringWithFormat:@"index_%i",row];
}
每当用户向上/向下滚动时,方法会自动触发,类似于的UITableView
。记住 UIPickerView
使用的是私有的 UIPickerTableView
,因此我们无法检测您想要的滚动方式。
whenever user is scrolling up/down above method is triggered automatically which is similar to UITableView
. Remember UIPickerView
is using UIPickerTableView
which is private, so we can not detect the scrolling the way you want to have.
让我解释一下检测pickerview中的方向。
Let me explain the detecting the direction in pickerview.
示例。可见行是 index_4,index_5,index_6,index_7
。现在,如果用户向下滚动将调用index_8
。同样,如果用户向上滚动将调用index_3
。
Example. Visible rows are index_4,index_5,index_6,index_7
. Now if user is scrolling down index_8
will be called. Similarly if user is scrolling to up index_3
will be called.
我希望这个技巧能解决你的问题。即使 - 请告诉我你对此的反馈。
I hope this trick will solve your problem. Even-though let me know your feedback on this.
这篇关于是否有像scrollviewDidScroll这样的uipickerview委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!