Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

iphone - Read position of UIPickerView while scrolling?

I have a UIPickerView with custom cells. For every moment, I'd like to know which cell is in the center location (behind the tinted selection indicator). The UIPickerView delegate method

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

does provide this information, but only after the picker view has stopped. What I'm looking for is a way to retrieve the currently "selected" cell while the picker view is still spinning and has not yet come to a halt.

As a work around, I've tried to go through KVO and register for each cell's view changes:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    .
    .
    [cell addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
}

I do receive callbacks to observeValueForKeyPath:ofObject:change:context:, however no matter whether I scroll the picker view up or down, the received values seem to be always the same:

2011-03-09 08:00:34.429 Project[35069:207] Frame: x=2.00, y=-13.00
2011-03-09 08:00:34.430 Project[35069:207] Location: x=4.00, y=-26.00
2011-03-09 08:00:34.430 Project[35069:207] Change: {
    kind = 1;
    new = "NSRect: {{2, -13}, {118, 70}}";
    old = "NSRect: {{2, -13}, {118, 70}}";
}
2011-03-09 08:00:34.431 Project[35069:207] Frame: x=2.00, y=-13.00
2011-03-09 08:00:34.431 Project[35069:207] Location: x=4.00, y=-26.00
2011-03-09 08:00:34.432 Project[35069:207] Change: {
    kind = 1;
    new = "NSRect: {{2, -13}, {118, 70}}";
    old = "NSRect: {{2, -13}, {118, 70}}";

(Location coordinates are in window frame) 

Any other ideas what I could try to get that value?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the method:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view`

with:

[pickerView selectedRowInComponent:0]);

Is exactly what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...