首页 文章

隐藏UIDatePicker的事件

提问于
浏览
1

我在 UIViewController 上有 UIDatePicker .

用户选择日期后点击 UIDatePicker 外我想隐藏 UIDatePicker 如:

-(void)hidePicker
{   
    [UIView beginAnimations:@"SlideOutPicker" context:nil];
    [UIView setAnimationDuration:0.5];
    [_datePicker setCenter: CGPointApplyAffineTransform(_datePicker.center, _datePicker.transform)];
    [_datePicker setTransform:CGAffineTransformMakeTranslation(0, 0)];
    [UIView commitAnimations];
}

我试试

[_datePicker addTarget:self action:@selector(hidePicker) forControlEvents:UIControlEventTouchUpOutside];

但事件没有发生,你能给我一些建议吗?

我不想使用 UIControlEventValueChanged ,因为每次用户更改日期时都不应隐藏 DatePicker

2 回答

  • 1

    您不能将 UIControlEventTouchUpOutside 用于此行为 . 在您的情况下,您需要在选择器外部创建透明视图或按钮,并为此设置操作以关闭选择器 . 将此视图/按钮的背景颜色设置为清晰颜色以实现此目的 . 点击按钮或视图,您可能不得不关闭选择器 .

    UIControlEventTouchUpOutside 主要用于触摸视图内部并移出手指 . 移出视图的边界时,它会触发事件 .

  • 1

    每次触摸屏幕时,只需检查触摸是否不在UIDatePicker的范围内 . 您是否尝试设置hidden = YES; ?我想你可以改变它在动画块中的可见性 . 顺便说一句,如果你正在为iOS 4开发,那么你最好使用new(它们实际上不再是新的)UIView类动画方法,因为Apple不鼓励使用beginAnimations和commitAnimations块 . 以下是我提到的那些方法:

    animateWithDuration:animations:
    animateWithDuration:animations:completion:
    animateWithDuration:delay:options:animations:completion:
    

    更多信息http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW111

相关问题