2012-02-08 76 views
4

我發現,如果我做到以下幾點:UIPickerView,檢測「滾輪」的啓動和停止?

  1. 單擊動畫一個UIPickerView到我的觀點
  2. 快速啓動按鈕,輪對,那麼過去的滾動,最後一個項目
  3. 辭退的觀點with a button

那麼它還沒有選擇最後一項呢。

我試過這個,只要輸出到控制檯,每當didSelectRow方法被觸發時,它會在輪子穩定在最後一個項目時觸發。

我可以檢測到車輪仍在滾動,這樣我就可以延遲檢查它是否有選定的值,直到它穩定下來爲止?如果有問題,我在MonoTouch中編程,但是如果您有一個代碼示例,我可以很好地閱讀Objective-C代碼以重新實現它。

回答

-1

我想你可以檢查UIPickerView是否處於動畫製作中,並等待它停止。這裏回答了這個問題link

+1

而且你會如何 「檢查」 的動畫關鍵點?你能提供一個更完整的答案,請詳細說明這種方法。我遇到你的答案,希望回答一個問題,但我現在要離開更多的問題。 – Pavan 2014-09-06 08:10:09

+0

AnimationKeys在2015年似乎不起作用。在我將代碼轉換爲Swift後,DrainBoy的答案適用於我。 – ObjectiveTC 2015-07-31 05:10:05

3

由於animationKeys似乎不工作了,我有另一種解決方案。如果您查看UIPickerView的子視圖,則會看到每個組件都有一個UIPickerTableView

這個UIPickerTableView確實是UITableView的子類,當然也是UIScrollView。因此,您可以檢查其值contentOffset以檢測差異。

此外,其scrollViewDelegate爲零默認情況下,所以我認爲你可以安全地設置你的目的是檢測scrollViewWillBeginDraggingscrollViewDidEndDecelerating

通過保持每個UIPickerTableView一個參考,你應該能夠實現高效的isWheelRolling方法。

-1

您可以在選取器上使用SwipeGestureRecognizer。 我認爲這不是一個完美的解決方案。

- (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"); 
} 
+0

這不起作用(測試)。 – Emilio 2014-07-14 22:08:58

7

由於動畫鍵不起作用,我寫了這個簡單的函數來檢測UIPickerView是否正在移動。

-(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; 
} 

它最終返回真正的五個深度。

+1

這對我有用。謝謝! – ObjectiveTC 2015-07-31 05:08:55

0

斯威夫特版本@DrainBoy的分機應答

extension UIView { 
func isScrolling() -> Bool { 

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

    for subview in self.subviews { 
     if (subview.isScrolling()) { 
      return true 
     } 
    } 
    return false 
} 
} 
1

擴展@iluvatar_GR回答

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()} 

    } 
} 
} 
0

擴展@iluvatar_GR,@Robert_at_Nextgensystems回答

使用手勢,UIScrollView的isDragging或isDecelerating 。

// Call it every time when Guesture action. 
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) { 
    // Changes the button name to scrolling at the start of scrolling. 
    DispatchQueue.main.async { 
     self._button.setTitle("Scrolling...", for: .normal) 
     self._button.isEnabled = false 
     self._button.backgroundColor = Utils.hexStringToUIColor(hex: "FF8FAE") 
    } 

    // Indication according to scrolling status 
    _datePicker.waitTillDoneScrolling(completion: { 
     print("completion") 
     DispatchQueue.main.async { 
      self._button.setTitle("Completion", for: .normal) 
      self._button.isEnabled = true 
      self._button.backgroundColor = Utils.hexStringToUIColor(hex: "7CB0FF") 
     } 
    }) 
} 

[SWIFT4]分享示例源鏈接!

enter Sample Source link