检测用户何时点击 UIDatePicker 中的选择指示器?

Posted

技术标签:

【中文标题】检测用户何时点击 UIDatePicker 中的选择指示器?【英文标题】:Detect when user taps the selection indicator in a UIDatePicker? 【发布时间】:2011-09-17 14:01:47 【问题描述】:

如何

如果没有这个,用户必须滚动到其他日期,然后再次返回以选择日期选择器向上滑动时显示在选择指示器下方的日期。

非常感谢, 斯汀

更新:这是我自己能想到的唯一解决方案:

UIDatePicker *aDatePicker = [[UIDatePicker alloc] init];
self.datePicker = aDatePicker;
[aDatePicker release];
[self.datePicker addTarget:self action:@selector(datePicked:) forControlEvents:UIControlEventValueChanged];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(datePicked:)];    
[self.datePicker addGestureRecognizer:tap];
[tap release];

这意味着datePicked会在用户实际转动滚轮时被调用两次。

更新:上述解决方案不适用于 UIPickerViews。在这些情况下,我不知道如何实现想要的行为。

【问题讨论】:

感觉做不到 :( 太烦人了! 我真的不明白你为什么想要这个。仅仅因为UIControlEventValueChanged 操作没有针对最初设置的日期触发?为什么不直接使用日期选择器的date 属性来获取初始选择?还是我完全没有抓住重点? +1,我完全同意@omz 我知道如何读取当前选择的日期,但我只对用户实际选择该日期感兴趣。 这里是答案***.com/questions/22319427/… 【参考方案1】:

您可以通过这种方式进行一些调整:-

在您的 .h 文件中符合委托 <UIGestureRecognizerDelegate>

UITapGestureRecognizer* gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerViewTapGestureRecognized:)];
[yourDatePicker addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate=self;
gestureRecognizer.numberOfTapsRequired=2;//Whenever you do double tap it will called. So allow user to do double tap on selected date.

//下面是Delegate方法

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

    return YES;

//双击时会触发以下方法

-(void)pickerViewTapGestureRecognized:(UITapGestureRecognizer*)recognizer

   UIDatePicker *datePicker=(UIDatePicker*)[[recognizer view] viewWithTag:101];
   NSLog(@"datePicker=%@", datePicker.date);
 

【讨论】:

【参考方案2】:

试试这个代码:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerViewTapped:)];

[recognizer setNumberOfTapsRequired:2];
[recognizer setCancelsTouchesInView:NO];
[recognizer setDelaysTouchesEnded:NO];
[recognizer setDelaysTouchesBegan:NO];

[self.answerPicker addGestureRecognizer:recognizer];

// ....

- (IBAction)pickerViewTapped:(UITapGestureRecognizer *)sender 
    CGPoint coord = [sender locationInView:self.answerPicker];
    if(coord.y <= 126 && coord.y >= 90) 
        //do something
    

【讨论】:

【参考方案3】:

这是一个老问题,但这是我为了在 UIDatePicker 上抓取选择栏矩形所做的。有了这个,你可以添加一个带有手势识别器的按钮/视图来检测点击。这有点 hack,但它似乎在 ios6 和 iOS7 中运行良好。

+ (CGRect)getSelectionBarRectFromPicker:(UIDatePicker *)picker

    int counter = 0;
    CGRect selectionBarRect;
    for(UIView *datePickerView in picker.subviews)
        for(UIView *subview in datePickerView.subviews)
            if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
                if([NSStringFromClass([subview class]) isEqualToString:@"_UIPickerViewSelectionBar"])
                    if(counter == 0)
                        selectionBarRect.origin = subview.frame.origin;
                        selectionBarRect.size.height = subview.frame.size.height;
                    
                    selectionBarRect.size.width += subview.frame.size.width;
                    counter++;
                
             else 
                if(subview.frame.size.height < 1)
                    if(counter == 0)
                        selectionBarRect.origin = subview.frame.origin;
                        selectionBarRect.size.width = subview.frame.size.width;
                     else 
                        selectionBarRect.size.height = subview.frame.origin.y - selectionBarRect.origin.y;
                    
                    counter++;
                
            
        
    
    return selectionBarRect;

【讨论】:

以上是关于检测用户何时点击 UIDatePicker 中的选择指示器?的主要内容,如果未能解决你的问题,请参考以下文章

检测用户何时点击 SKSpriteNode

观察 UIDatePicker 的变化

检测 AVPlayerViewController 完成按钮点击?

通过Swift中的触摸检测应用程序何时处于活动状态或非活动状态[重复]

iOS 9:如何检测用户何时对推送通知请求说“不允许”? [复制]

如何检测何时单击/点击 MapMarker? [复制]