Event to hide UIDatePicker - objective-c

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

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.

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

Related

UIButton inside a UIView not working after UIView is animated

I have seen many questions in this forum which gives answer to this topic "UIButton inside a UIView, when animated doesn't work", but after having tried out the answers like
a) UIViewAnimationOptionAllowUserInteraction to the options
b) subView.setUserInteractionEnabled = YES
c) [button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
none of them is working for me :-(
Here is the scenario. App is in landscape mode and a UIView called menuView is placed at x=480,y=0. Its height=200 and width=150. So, when I tap the button on the top right corner, the following code is executed
- (IBAction)showMenu {
[menuView setUserInteractionEnabled:YES];
[optionsButton setUserInteractionEnabled:YES];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, 200);
}
completion:NULL];
}
Output?: View is appearing, but optionsButton inside the menuView is not clickable. I hooked up to an event, did an NSLog(#"Options button clicked"), but nothing happens :-(
Please tell me what I am doing wrong?
Update: When I place the subview that is "menuView" inside the self.view, then upon running and click the optionsButton, I get the NSLog message. Only if I specify the origin.x of menuView to be 480 and above, it doesn't work.
If you can see the UIButton, its userInteractionEnabled (YES by default!) is set, and its superview's userInteractionEnabled in the whole hierarchy is YES - you should be able to interact with it.
An animation will never change your userInteractionEnabled property! So what you are doing there is simply unnecessary. If you try to enable interaction during animation - that's a different story and is just an option passed to the animation message.
So if that's not your problem (and is probably not), then I guess one of the superview's frame is cropping the UIButton!
Now you see, if a UIButton (or any UIView) is outside a UIView's frame, it can still be visible, unless clipsToBounds is set on the superview.
And the outcome of that situation is: You can see me, but you can't touch me.
In my case missing a constraint on the height of the containing view caused the view height to be 0. Buttons were visible, below the view, but untouchable.
For me it was all about an overlooked mistake in the animation code itself. Where my new height was being called I was actually calling my new width, and where my new width was being called I was actually calling my new height, therefor causing the UIView to be super long yet super narrow, thus pushing all content within the center of view, outside of the view reach.
Change your UIViews background color to Red so that you can actually see where it animates to.
If your UIView is within another view, turn on clipSubviews so that you only see what is actually enabled within the subView.
Run your project.
If your UIView is not its normal height and width, more than likely you have overlooked some code in your animation call.
Make sure that your x, y, width, height are being shown in the correct orders within the animation code.
In my case, it seems like my animated transitioning is not yet completed. I forgot to to put completeTransition:Bool at the end of my animateTransition:context method.

How to pinch view on button press?

I've got the button on the fullscreen UIImageView and I want to pinch this view on that button press. I mean, I want to show only the part of the UIImage to the fullscreen after the button is pressed. Which method should I use?
Basically you can just start a zoom Animation, if that is all what you want.
Example:
[UIView animateWithDuration:1.0 animations:^{
yourView.transform = CGAffineTransformMakeScale(2.0,2.0);
}];
Put your UIImageView inside a UIScrollView to get zooming and panning.
See this SO post. I've already used this technique and it's the only way I know to do it.
you will want to use the pinchGestureRecognizer :)
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPinchGestureRecognizer_Class/Reference/Reference.html

How can I start animating an UILabel from a particular location in iPad?

I need to start animating a UILabel when a button is pressed... the animation should start from the position of the button.
How can I achieve this?
i.e moving label from one end of the screen to another end.
This is acheviced with UIView's animateWithDuration methods, which can be found in the documentation here.
A very simple example which should be called when your button is pressed:
[UIView animateWithDuration:0.5 animations:^{
// set new position of label which it will animate to
myLabel.frame = CGRectMake(x,y,width,height);
}];

How to implement the effect of popping up tableview from bottom of screen?

I'm a newbie of Xcode and Objective-c. Recently I wanna do a task about popping up tableviews. When a button is pressed, the tableview should pop up from the bottom of the screen. The tableview will contain just a few items so I don't wanna it occupy full screen. I read the manual and found UIModalPresentationStyle, but it seems not fulfill my requirements. So what's the more accurate methods I can use?
While #Bharat J's answer is correct, those methods are deprecated in iOS 4.0.
The latest and greatest implementation is + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
Implemented as:
[UIView animateWithDuration:0.5 animations: ^{
tableView.frame = newFrame;
}
];
You can use below code to create an animation block . Set the frame for the table view as (0,480,320,0) When you hit a button change the frame of the table view in the animation block and make it to CGRectMake(0,200,320,280) or something .
[UIView beginAnimations:#"AnimateTableView" context:view];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tableView.frame = CGRectMake(0, 200 , 320, 280);
[UIView commitAnimations];
The same animation block for hiding it again but with the frame again begin set to CGRectMake(0,480,320,0). This should work in your case .
Another option would be to use a UIActionSheet and add the uitableview as a subview.
Here is a very similar question about how to add a UIPickerview with similar effect.
Add UIPickerView & a Button in Action sheet - How?

UIColor groupTableViewBackgroundColor is transparent

I have a grouped style UITableView on my navigation stack, and when I click on a cell, I push a UIDatePicker onto the stack. The problem is that I want this custom view to have the same background color as my table view.
I tried setting the background color of my custom view like:
datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
But this comes out transparent. I also tried modifying the underlying CGColor object to have an alpha of 1.0, which caused the background color to be black.
The following does work as expected:
datePicker.backgroundColor = [UIColor lightGrayColor];
But, of course, this color doesn't quite match the grouped table background color.
Am I going about this all wrong? I found a similar post about this here, but no helpful response.
What do you mean you "push a UIDatePicker onto the stack"? Why dont you try animating the UIDatePicker into view?
when the view loads, create the picker and set the frame off screen, such as
[picker setFrame:CGRectMake(0,960,320,216)];
then instead of "pushing" the picker, animate it into view like:
[UIView beginAnimations:nil context:NULL];
[picker setFrame:CGRectMake(0,200,320,216)];
[UIView commitAnimations];
And when you want to dismiss the picker, just hide it like:
[UIView beginAnimations:nil context:NULL];
[picker setFrame:CGRectMake(0,960,320,216)];
[UIView commitAnimations];
If you need to, you can also add a toolbar with a "done" button to dismiss the picker, works great for me.
If the contents of the picker are going to be displayed on the table, then you can set the frame of the table in that animation sequence. in the first one, make the table half the size (like 150 for my example would work perfect), then in the hide sequence, make the table the original size (415 for this example). And when you hide the picker, call [tableView reloadData]; to refresh the table.