1

I've got UIDatePicker on UIViewController.

After user selected the date and clicked outside the UIDatePicker I would like hide UIDatePicker like:

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

I try

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

but event doesn't happen, can you get me some advise?

I don't wanna use UIControlEventValueChanged because DatePicker should not hide each time when user change the date

iDev
  • 23,310
  • 7
  • 60
  • 85

2 Answers2

1

You cannot use UIControlEventTouchUpOutside for this behavior. In your case you need to create a transparent view or button outside the picker and set action for that to dismiss the picker. Set the background color of this view/button as clear color to achieve this. On tap of the button or view, you might have to dismiss the picker.

UIControlEventTouchUpOutside is mainly used for touching inside the view and moving out the finger. While moving out of the bounds of the view, it triggers the event.

iDev
  • 23,310
  • 7
  • 60
  • 85
1

Just check if the touch is not inside the bounds of the UIDatePicker each time you touch the screen.Have you tried to set hidden=YES; ? I guess you can change its visibility inside the animation block. BTW, if you're developing for iOS 4+ then you'd better use new (they're actually not new any more) UIView class methods methods for animations because using beginAnimations and commitAnimations blocks is discouraged by Apple. Below are those methods I mentioned:

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

More information in http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW111

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206