Picker View not appearing on 1st attempt of button click - iphone

I have a button on the extreme right of the cell.
Now I have a picker view which needs to be displayed on button click.
Its working fine with the following code:
-(IBAction)buttonPressed:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
monthPicker.transform = transform;
[self.view addSubview:monthPicker];
[UIView commitAnimations];
}
But I searched for making picker view hide when it is visible and be shown when no picker view is present,I found out a solution for it.....added the following code to above method:
monthPicker.hidden = [monthPicker isHidden] ? NO : YES;
Now the picker view is not getting displayed on 1st attempt when I click button,but it's working perfect from 2nd attempt and later.
Any suggestions?

The answer from #Padavan should work, but if you want fading effect "in animation", hidden property does not do the job. You have to use UIView.alpha property instead. Here is my sample code for that.
- (void) viewDidLoad {
[super viewDidLoad];
/////////////////////////////////////////////////
// ... Initialize your month picker here //
/////////////////////////////////////////////////
monthPicker.alpha = 0;
[self.view addSubview:monthPicker];
}
- (void)buttonPressed:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
monthPicker.transform = transform;
monthPicker.alpha = monthPicker.alpha * (-1) + 1;
[UIView commitAnimations];
}

Are monthPicker initially hidden? When method called first time. If no - it hides and you can't see it. Maybe you need set monthPicker.hidden=YES in init method.

Related

how to trigger uiview animation to wok again

i have an animation that works in the main menu screen of an iPad application.
when select some menu item, the application will push the new view the navigation controller.
the problem happens when i want to restart the animation again in the moment of when i push the back button and return to the main menu again.
i tried to put the animation code in these methods :
-(void) viewDidLoad
-(void) viewDidAppear
but i can't get them to work.
what i am trying to do is animation the company's logo in the background .
my code is :
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:15.0];
[UIView setAnimationRepeatCount:20.0f];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos = large_bright.center;
pos.x = 400.0f;
large_bright.center = pos;
CGPoint pos2 = large_dim.center;
pos2.x = -10.0;
large_dim.center = pos2;
[UIView commitAnimations];
you should call
(void)viewWillAppear:(BOOL)animated;
Try:
[UIView animateWithDuration:1.0 // set here how fast u want it to animate
animations:^{
// add your needed animation here
}
completion:^(BOOL finished) {
// do some stuff when its done if you need to
}
];
Call some other function from viewDidLoad and viewDidAppear.and then add your animation code in that function.
-(void) viewDidLoad
{
[self add_animation];
}
-(void)animation
{
//your animation code
}

I have a UITextField that gets hidden by the keyboard when selected. How can it be brought into view?

I have a UITableView that is shorter than the window, therefore it does not need to scroll. However, it is long enough that when a text field in the bottom row is selected, the keyboard covers it.
I can't use scrollToRowAtIndexPath because the table is shorter than the window, so I was wondering what the correct way to bring it into view would be.
I was thinking about sliding the whole view up a set number of pixels, although that seems very bad form because it would break the UI if I added more rows to the table.
You should implement these methods in the concerned class :
- (void) textFieldDidBeginEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movement = (up ? -105 : 105);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
You have to adapt values (-105, 105 and 0.3f) to your situation.
You can have the whole tableView slide up by setting the height of the footerView. The Keyboard will move the table above for the height of the footer
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 70.0;
}
Pierre's code worked for me, but I removed the myTextField argument. It seemed unnecessary. I also changed this over to UITextViews because that's what I had in my code. Otherwise, thanks for the question and answers. I've been beating my head against the wall to solve this problem!
#pragma mark - Text View Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextViewUp:YES];
}
- (void) textViewDidEndEditing:(UITextView *)textView
{
[self animateTextViewUp:NO];
}
- (void) animateTextViewUp:(BOOL)up
{
int movement = (up ? -80 :80);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

Value of button Title animating in Core Animation Block

I am using a UIButton to represent number of notification on bar of a table that slides up/down when the button is placed. The title of the button changes dynamically to represent number of un-read notifications.
Please see partial screenshots attached.
Start Frame of Animation - Notifications showing as 2 - Correctly
End Frame of Animation - Notifications Showing as 0 - This should not change
Below is the code I am using to animate
-(IBAction) showNotifications {
if (notificationTableIsUp) {
CGRect notificationsHeaderFrame = notificationsHeader.frame;
CGRect notificationsTableFrame = notificationsTable.frame;
// Animate to pull up Notification Table
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
notificationTableIsUp = !notificationTableIsUp;
notificationsHeaderFrame.origin.y += 220;
notificationsHeader.frame = notificationsHeaderFrame;
notificationsTableFrame.origin.y += 220;
notificationsTable.frame = notificationsTableFrame;
[UIView commitAnimations];
} else {
CGRect notificationsHeaderFrame = notificationsHeader.frame;
CGRect notificationsTableFrame = notificationsTable.frame;
// Animate to pull down Notification Table
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
notificationTableIsUp = !notificationTableIsUp;
notificationsHeaderFrame.origin.y -= 220;
notificationsHeader.frame = notificationsHeaderFrame;
notificationsTableFrame.origin.y -= 220;
notificationsTable.frame = notificationsTableFrame;
[UIView commitAnimations];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
notificationTableIsUp = NO;
[self updateNotificationBadgeTo:2];
}
- (void) updateNotificationBadgeTo:(NSInteger)numberOfNotifications {
notificationBadgeButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
notificationBadgeButton.titleLabel.textColor = [UIColor whiteColor];
notificationBadgeButton.titleLabel.text = [NSString stringWithFormat:#"%d", numberOfNotifications];
}
Note that the Green bar is a UIView and Notifications is a UILabel and Round button is a UIButton all set up in IB. 0 is the initial title for the button set in IB. I am setting it to 2 in viewDidLoad.
So basic problem here is when I click on the button, the value it changing from white 2 (set in viewDidLoad) to Blue 0 (Set in IB).
How can I remove this part when I am animating the slide up/down?
Thanks
Dev.
No Solution found so far. Ended up removing this feature altogether :(

How can I present a UIView from the bottom of the screen like a UIActionSheet?

I'd like a UIView to slide up from the bottom of the screen (and stay mid-screen) like a UIActionSheet. How can I accomplish this?
UPDATE:
I am using the following code:
TestView* test = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
test.view.center = CGPointMake(160,100);
//test.view.frame = CGRectMake(0, 0, 160, 210);
[[[UIApplication sharedApplication] keyWindow] addSubview:test.view];
[UIView commitAnimations];
The view seems to be animating from the corner and appearing in the corner. How can I make it slide up from the bottom? Getting close!
Do what Matt did here, but just change the values and direction. I have code at home to do this from the bottom if you need it later (I'll update this post).
Link: http://cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html
Also, don't forget to take out the bit of code that shifts the main view downward (so instead the UIView just pops over top like an ActionSheet)
Updated with code:
This is what I use in one of my apps to show/hide a little "options" view:
- (void)toggleOptions:(BOOL)ViewHidden
{
// this method opens/closes the player options view (which sets repeat interval, repeat & delay on/off)
if (ViewHidden == NO)
{
// delay and move view out of superview
CGRect optionsFrame = optionsController.view.frame;
[UIView beginAnimations:nil context:nil];
optionsFrame.origin.y += optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[UIView commitAnimations];
[optionsController.view
performSelector:#selector(removeFromSuperview)
withObject:nil
afterDelay:0.5];
[optionsController
performSelector:#selector(release)
withObject:nil
afterDelay:0.5];
optionsController = nil;
}
else
{
optionsController = [[PlayOptionsViewController alloc] init];
//
// Position the options at bottom of screen
//
CGRect optionsFrame = optionsController.view.frame;
optionsFrame.origin.x = 0;
optionsFrame.size.width = 320;
optionsFrame.origin.y = 423;
//
// For the animation, move the view up by its own height.
//
optionsFrame.origin.y += optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[window addSubview:optionsController.view];
[UIView beginAnimations:nil context:nil];
optionsFrame.origin.y -= optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[UIView commitAnimations];
}
}
One way would be to use the present modal view controller on the view controller:
presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
For more info take a look at the UIViewController documentation.
EDIT: If you want a mid-screen view you'll need to animate it into position as #jtbandes has pointed out. I suggest also adding some candy to UIView animation block:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myView.center = CGPointMake(x,y);
[UIView commitAnimations];
You can then move it again if you need to go full screen or dismiss it.
You'll have to move the view yourself, by setting its center or frame. I'll let you figure out what to set those to. But for the animation:
// set the view to its initial position here...
[UIView beginAnimations:nil context:NULL];
// move the view into place here...
[UIView commitAnimations];
Check out this post: http://blog.yetanotherjosh.com/post/33685102199/3-ways-to-do-a-vertical-transition-with
I'm going with the modal window approach.
Try this solution.... it works
#pragma mark - Date Selector View PresentModelView with Transparent ViewController
- (void) showModal:(UIView*) modalView {
CGRect rect=modalView.frame;
rect.origin=CGPointMake(0, 0);
self.tutorialView.frame=rect;
UIWindow *mainWindow = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
CGPoint middleCenter;
middleCenter = CGPointMake(modalView.center.x, modalView.center.y);
CGSize offSize = [UIScreen mainScreen].bounds.size;
CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
modalView.center = offScreenCenter;
if ([[mainWindow subviews] containsObject:modalView]) {
[modalView removeFromSuperview];
}
[mainWindow addSubview:modalView];
[mainWindow bringSubviewToFront:modalView];
// Show it with a transition effect
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
// animation duration in seconds
modalView.center = middleCenter;
[UIView commitAnimations];
}
// Use this to slide the semi-modal view back down.
- (void) hideModal:(UIView*) modalView {
CGSize offSize = [UIScreen mainScreen].bounds.size;
CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
[UIView beginAnimations:nil context:(__bridge void *)(modalView)];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(hideModalEnded:finished:context:)];
modalView.center = offScreenCenter;
[UIView commitAnimations];
}
- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIView *modalView = (__bridge UIView *)context;
[modalView removeFromSuperview];
}

Make textfield appear from above in an animated way

Is it possible to make textfield come from above to down in an animated way when a button is pressed?
Yes. You must chnage the frame of the UITextField inside an animation block. Something like this. Assuming, for example, that the Y origin of the text field before pressing the button is 0.0f (top of its container).
- (void) buttonPressed {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: <desired_animation_duration_in_seconds> ];
CGRect aFrame = _myTextField.frame;
aFrame.origin.y = <desired_y_coord>;
_myTextField.frame = aFrame;
[UIView commitAnimations];
}