简体   繁体   English

UIScrollView内的UIDatePicker-iPhone应用程序

[英]UIDatePicker inside of a UIScrollView - iPhone application

How can I use a UIDatePicker inside of a UIScrollView in my iPhone app. 如何在iPhone应用程序的UIScrollView中使用UIDatePicker。 I can't scroll through the dates on the picker, because it thinks my finger is controlling the UIScrollView instead. 我无法滚动选择器上的日期,因为它认为我的手指正在控制UIScrollView。 I'm trying to add a UIDatePicker to my UIScrollView, which is inside of my UIViewController. 我试图将一个UIDatePicker添加到我的UIScrollController中,它位于我的UIViewController内部。

You might try setting the scroll view's delaysContentTouches property to NO. 您可以尝试将滚动视图的delaysContentTouches属性设置为NO。

Better, though, would be to rethink your UI. 更好的办法是重新考虑您的UI。 A date picker takes up about half the screen on a small device. 在小型设备上,日期选择器占据了大约一半的屏幕。 Even if you can get the date picker to scroll instead of the scroll view, you'll put the user in a position where it's going to be hard to scroll the scroll view. 即使可以让日期选择器滚动而不是滚动视图,也可以将用户置于一个难以滚动滚动视图的位置。 They'll have to figure out that they'll need to touch above or below the picker if they want to scroll the scroll view... it's just not going to be nice to use. 他们必须弄清楚,如果要滚动滚动视图,则需要触摸选择器的上方或下方……这不会很好用。

One solution might be to let the user tap a date, and then to present a modal view controller with a date picker. 一种解决方案可能是让用户点击日期,然后为模式视图控制器提供一个日期选择器。 That'd let the user pick a new date without any ambiguity about scrolling. 这样一来,用户就可以选择一个新的日期,而不必担心滚动问题。

After a few trials, I found the best solution to this problem was to subclass the scroll view that contains your date picker and do a hit test to see whether the user is trying to touch the picker. 经过几次试验,我发现此问题的最佳解决方案是将包含日期选择器的滚动视图子类化,并进行点击测试,以查看用户是否尝试触摸选择器。 I found and adapted the code below from this question UIDatePicker inside UIScrollView with pages 我从带有页面的UIScrollView中的这个问题UIDatePicker找到并改编了以下代码

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if ([result.superview isKindOfClass:[UIPickerView class]] || [result.superview isKindOfClass:[UIDatePicker class]])
    {
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = NO;
    }
    else 
    {
        self.canCancelContentTouches = YES; // (or restore bool from prev value if needed)
        self.delaysContentTouches = YES;    // (same as above)
    }
    return result;
}

another solution is pop it upward from bottom when you want to select data from picker. 要从选择器中选择数据时,另一解决方案是从底部向上弹出。 add to ur view (self.view). 添加到您的视图(self.view)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM