UIPickerView, detect “rolling wheel” start and sto

2019-02-13 22:46发布

I just discovered that if I do the following:

  1. Click the button that animates a UIPickerView into my view
  2. Quickly start the wheel rolling towards, then past, the last item
  3. Dismiss the view with a button

Then it has not yet selected the last item yet.

I tried this by simply outputting to the console whenever the didSelectRow method was fired, and it fires when the wheel stabilizes on the last item.

Can I detect that the wheel is still rolling, so that I can delay checking it for a selected value until it stabilizes?

If it matters, I'm programming in MonoTouch, but I can read Objective-C code well enough to reimplement it, if you have a code example that is.

7条回答
Bombasti
2楼-- · 2019-02-13 23:07

Expanded @iluvatar_GR answer

extension UIView { 
func isScrolling () -> Bool {

    if let scrollView = self as? UIScrollView {
        if (scrollView.isDragging || scrollView.isDecelerating) {
            return true
        }
    }

    for subview in self.subviews {
        if ( subview.isScrolling() ) {
            return true
        }
    }
    return false
}
func waitTillDoneScrolling (completion: @escaping () -> Void) {
    var isMoving = true
    DispatchQueue.global(qos: .background).async {
    while isMoving == true {
        isMoving = self.isScrolling()
    }
        DispatchQueue.main.async {
            completion()}

    }
}
}
查看更多
成全新的幸福
3楼-- · 2019-02-13 23:08

Swift 4 (updated) version with extension of @DrainBoy answers

extension UIView  {
 func isScrolling () -> Bool {

    if let scrollView = self as? UIScrollView {
        if  (scrollView.isDragging || scrollView.isDecelerating) {
            return true
        }
    }

    for subview in self.subviews {
        if ( subview.isScrolling() ) {
            return true
        }
    }
    return false
 }
}
查看更多
来,给爷笑一个
4楼-- · 2019-02-13 23:10

You can use a SwipeGestureRecognizer on the picker. I assume this is not a perfect solution at all.

- (void)viewDidLoad
{
    [super viewDidLoad];

    _pickerSwipeGestureRecognizer.delegate = self;
    [_pickerSwipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp)];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if([gestureRecognizer isEqual:_pickerSwipeGestureRecognizer]){
        NSLog(@"start");
    }
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"end");
}
查看更多
劫难
5楼-- · 2019-02-13 23:20

As animation keys don't work, I wrote this simple function that works for detecting if a UIPickerView is currently moving.

-(bool) anySubViewScrolling:(UIView*)view
{
    if( [ view isKindOfClass:[ UIScrollView class ] ] )
    {
        UIScrollView* scroll_view = (UIScrollView*) view;
        if( scroll_view.dragging || scroll_view.decelerating )
        {
            return true;
        }
    }

    for( UIView *sub_view in [ view subviews ] )
    {
        if( [ self anySubViewScrolling:sub_view ] )
        {
            return true;
        }
    }

    return false;
}

It ends up returning true five levels deep.

查看更多
孤傲高冷的网名
6楼-- · 2019-02-13 23:20

Since animationKeys seems to not work anymore, I have another solution. If you check the subviews of UIPickerView, you'll see that there is a UIPickerTableView for each component.

This UIPickerTableView is indeed a subclass of UITableView and of course of UIScrollView. Therefore, you can check its contentOffset value to detect a difference.

Besides, its scrollViewDelegate is nil by default, so I assume you can safely set an object of yours to detect scrollViewWillBeginDragging, scrollViewDidEndDecelerating, etc.

By keeping a reference to each UIPickerTableView, you should be able to implement an efficient isWheelRolling method.

查看更多
小情绪 Triste *
7楼-- · 2019-02-13 23:26

I think you can just check if the UIPickerView is in the middle of animating and wait for it to stop. This was answered here link

查看更多
登录 后发表回答