cannot move frame inside method inside textfielddidbeginediting - objective-c

I am trying to move textfield frame when you begin editing; hacked it by putting method call in "keyboardWasShown". But that is delayed suboptimal solution. How can I move textfield around from method inside of didBeginEditing? The method gets called but it just seems to ignore moving the frame around. Interestingly, the frame moves correctly the second time I interact with the commentField. Code:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:self.commentField]) {
self.commentField.text=#"";
[self.commentField setTextColor:[UIColor blackColor]];
[self animateTextFieldUp:self.commentField];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextFieldDown:self.commentField];
}
CGFloat KEYBOARD_HEIGHT;
- (void)keyboardWasShown:(NSNotification*)notification {
// Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
KEYBOARD_HEIGHT=keyboardSize.height;
[self animateTextFieldUp:self.commentField];
}
-(void)animateTextFieldUp:(UITextField*)textField
{
int movement = movementDistance;
[UIView beginAnimations: #"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.commentField.frame = CGRectOffset(self.commentField.frame, 0, movement);
self.bottomView.frame = CGRectOffset(self.bottomView.frame, 0, movement);
self.sendButton.frame = CGRectOffset(self.sendButton.frame, 0, movement);
[UIView commitAnimations];
}

Related

Animation in UITextField delegate method does not execute

When I click on text box txtAge, the delegate method gets called but animation does not execute:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"field Edited");
if (textField.tag == 0) {
NSLog(#"IF Block ");
[UIPickerView beginAnimations:nil context:nil];
[UIPickerView setAnimationDuration:0.3];
PkrView .frame = CGRectMake(0, 450, 414, 215);
}
else
{
NSLog(#" else");
}
return; }
Can you figure out why?
You missed commitAnimations call. However, quoted from Apple documentation about beginAnimations:
Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.
You should convert your animation like the following:
[UIPickerView animateWithDuration:0.3 animations:^{
self.view.frame = CGRectMake(0, 450, 414, 215);
} completion:^(BOOL finished){
// optional, do something after animation completed
}];

Hide UINavBar and UITabBar at the same time with animation

Edit: I awarded the bounty to john since he put a lot of effort into his answer, and would get it anyways, but there's still no working solution. I am still looking for an answer, if someone knows how to do this it'd be greatly appreciated.
I want to add a "maximize" button to my app that hides the navigation and tab bar. The navbar and tabbar should slide in/out smoothly, and the inner/content view should also expand and shrink at the same rate as the navbar and tabbar.
I used [self.navigationController setNavigationBarHidden: YES/NO animated: YES]; for the navbar and found this thread How to hide uitabbarcontroller for hiding the tabbar.
UITabBar class extension:
- (void) setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
CGRect screenRect = [[UIScreen mainScreen] bounds];
float screenHeight = screenRect.size.height;
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
screenHeight = screenRect.size.width;
}
if (!hidden) {
screenHeight -= self.tabBar.frame.size.height;
}
[UIView animateWithDuration: (animated ? UINavigationControllerHideShowBarDuration : 0) animations: ^{
for (UIView* each in self.view.subviews) {
if (each == self.tabBar) {
[each setFrame: CGRectMake(each.frame.origin.x, screenHeight, each.frame.size.width, each.frame.size.height)];
} else {
[each setFrame: CGRectMake(each.frame.origin.x, each.frame.origin.y, each.frame.size.width, screenHeight)];
}
}
} completion: ^(BOOL finished) {
NSLog(#"Animation finished %d", finished);
}];
}
The problem is when I use the two at the same time (hiding/showing the nav and tab bar), it's not clean. If the navbar comes first, anything anchored to the bottom jumps (see example below), and if the tabbar comes first, the top jumps.
Example: I position the UIButton in the bottom right and set its autoresizing mask
resizeButton.frame = CGRectMake(self.view.bounds.size.width - 50, self.view.bounds.size.height - 100, 32, 32); // hardcoded just for testing purposes
resizeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
But when the navbar and tabbar are minimized the UIButton jumps between the two states (doesn't slide along with the tab bar). However, if I change it to attach to the top right, it slides perfectly with the nav bar.
Does anyone know how to solve this?
Edit:
This is the closet and most elegant solution I have so far (just trying to get a working concept):
[UIView animateWithDuration: UINavigationControllerHideShowBarDuration animations: ^{
if (self.isMaximized) {
self.tabBarController.view.frame = CGRectMake(0, 20, screenRect.size.width, screenRect.size.height + 49 - 20);
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
self.tabBarController.view.frame = CGRectMake(0, 20, screenRect.size.width, screenRect.size.height - 20);
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
} completion: ^(BOOL finished) {
NSLog(#"Frame done: %#", NSStringFromCGRect(self.view.frame));
return;
}];
On maximizing:
Slides the navbar up, and slides the tabbar down, at the same time
The top of the inner/content view slides up, and the bottom of this view jumps down
On minimizing:
Slides the navbar down, and slides the tabbar up, at the same time
The top of the inner/content view slides down properly, but the bottom jumps to the final value, leaving whitespace which is then covered by the sliding tabbar
If I rearange the order of the minimizing-animations (so the navbar animatino is called first), then the top in the inner/content view jumps
the solution i use should eliminate the jump problem you see.
this solution is derived from an Objective-C category found Carlos Oliva's github page, and while the copyright in that code is "all rights reserved", i wrote him and he provided permission for use.
my category code varies only slightly from his code. also, find below the category code the invocation code that i use in my app.
from UITabBarController+HideTabBar.m
// the self.view.frame.size.height can't be used directly in isTabBarHidden or
// in setTabBarHidden:animated: because the value may be the rect with a transform.
//
// further, an attempt to use CGSizeApplyAffineTransform() doesn't work because the
// value can produce a negative height.
// cf. http://lists.apple.com/archives/quartz-dev/2007/Aug/msg00047.html
//
// the crux is that CGRects are normalized, CGSizes are not.
- (BOOL)isTabBarHidden {
CGRect viewFrame = CGRectApplyAffineTransform(self.view.frame, self.view.transform);
CGRect tabBarFrame = self.tabBar.frame;
return tabBarFrame.origin.y >= viewFrame.size.height;
}
- (void)setTabBarHidden:(BOOL)hidden {
[self setTabBarHidden:hidden animated:NO];
}
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
BOOL isHidden = self.tabBarHidden;
if (hidden == isHidden)
return;
UIView* transitionView = [self.view.subviews objectAtIndex:0];
if (!transitionView)
{
#if DEBUG
NSLog(#"could not get the container view!");
#endif
return;
}
CGRect viewFrame = CGRectApplyAffineTransform(self.view.frame, self.view.transform);
CGRect tabBarFrame = self.tabBar.frame;
CGRect containerFrame = transitionView.frame;
tabBarFrame.origin.y = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
containerFrame.size.height = viewFrame.size.height - (hidden ? 0 : tabBarFrame.size.height);
[UIView animateWithDuration:kAnimationDuration
animations:^{
self.tabBar.frame = tabBarFrame;
transitionView.frame = containerFrame;
}
];
}
from my ScrollableDetailImageViewController.m
- (void)setBarsHidden:(BOOL)hidden animated:(BOOL)animated
{
[self setTabBarHidden:hidden animated:animated];
[self setStatusBarHidden:hidden animated:animated];
// must be performed after hiding/showing of statusBar
[self.navigationController setNavigationBarHidden:hidden animated:animated];
}
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
{
id parent = self.navigationController.parentViewController;
if ([parent respondsToSelector:#selector(isTabBarHidden)]
&& hidden != [parent isTabBarHidden]
&& [parent respondsToSelector:#selector(setTabBarHidden:animated:)])
[parent setTabBarHidden:hidden animated:animated];
}
Just try this code, If it is working. I have written this code a year before. But, still it works good for me.
I didn't used the block based animations. Because it was written when I am new to iOS. Just try and optimize yourself as you wanted.
- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
[self.navigationController setNavigationBarHidden:YES animated:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
for(UIView *view in tabbarcontroller.view.subviews)
{
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}else if([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 1024, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 1024)];
}
}
}
[UIView commitAnimations];
}
// Method shows the bottom and top bars
- (void) showTabBar:(UITabBarController *) tabbarcontroller {
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
for(UIView *view in tabbarcontroller.view.subviews)
{
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 430, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 436)];
}
}else if([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 975, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 980)];
}
}
}
[UIView commitAnimations];
}
Try this
you can hide tabbar contrller and navigation bar using animation like:-
-(IBAction)hide:(id)sender
{
[self hideShowBars];
}
- (void) hideShowBars
{
CGRect rect = self.navigationController.navigationBar.frame;
CGRect rect1 = self.tabBarController.tabBar.frame;
if(self.navigationController.navigationBar.isHidden)
{
[self.navigationController.navigationBar setHidden:NO];
rect.origin.y=self.view.frame.origin.y;
}
else
{
rect.origin.y=self.view.frame.origin.y-rect.size.height;
}
if(self.tabBarController.tabBar.isHidden)
{
[self.tabBarController.tabBar setHidden:NO];
rect1.origin.y=self.view.frame.size.height-rect1.size.height-rect1.size.height;
}
else
{
rect1.origin.y=self.view.frame.size.height;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.50];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(hideShowBarsAnimationStopped)];
self.navigationController.navigationBar.frame=rect;
self.tabBarController.tabBar.frame = rect1;
[UIView commitAnimations];
}
- (void) hideShowBarsAnimationStopped
{
if(self.navigationController.navigationBar.frame.origin.y==self.view.frame.origin.y)
return;
if(!self.navigationController.navigationBar.isHidden)
{
[self.navigationController.navigationBar setHidden:YES];
}
if(!self.tabBarController.tabBar.isHidden)
{
[self.tabBarController.tabBar setHidden:YES];
}
}

List of Buttons lighting up at random

The method im trying to achieve is set a button to 0.5 alpha then back to 1 in the space of three seconds. After its executed 5 time on the four buttons the block of code is finished. Im struggling to find a way in which this can be achieved becasue right now the block below will be an infinite loop when i want it to be only executed through once.
int rand=random()%5;
switch (rand) {
case 1:{
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnYellow setAlpha:0.5];
[btnYellow setAlpha:1];
[UIView commitAnimations];
}
break;
case 2:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnRed setAlpha:0.5];
[btnRed setAlpha:1];
[UIView commitAnimations];
break;
case 3:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnBlue setAlpha:1];
[UIView commitAnimations];
case 4:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnGreen setAlpha:1];
[UIView commitAnimations];
break;
case 5:
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:5.0];
[btnYellow setAlpha:1];
[UIView commitAnimations];
break;
}
As you are setting up the button's alpha to 0.5 and then to 1 immediately, the button's alpha won't animate. You can get an idea this snippet
[UIView animateWithDuration:0.3 animations:^{
[your_btn setAlpha:0.5];
} completion:^(BOOL finished) {
if(finished)
[self performSelector:#selector(revertAlphaToOne) withObject:nil afterDelay:0.5];
}];
And in that revertAlphaToOne Method, you can revert the button's alpha to 1 as
[UIView animateWithDuration:0.3 animations:^{
[your_btn setAlpha:1.0];
} completion:nil
}];
Adjust the time variables according to your likings And/Or call the second snippet in the first block's completion block itself.
Well, I've been using this site for several years, and I was due for a nice thorough answer to give back.
I built a project and accomplished the functionality you desired. Some notes:
-The key is recursion! You need to animate the fade out (using the function I provided) and, upon completion, animating the fade back in using the same function. Once completed, exercise recursion and prepare to call itself (beginAnimations) until all ints=5.
-I decided to fade from 1->.5->1 because the .5 alpha was annoying me. Shouldn't be hard to switch that around if you want it reversed.
-To make sure no button fades more than fives times, you need to declare and increment an int corresponding to each button.
-Use arc4random(), not random(). Random is for debugging as you will have the same result every time.
-Keep your switch case's straight; was hard to understand what you wanted to be different between them. On this note, use breaks and a default statement! Good luck employing all this into your app.
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
//declare and define button specifics
btnYellow = [UIButton buttonWithType: UIButtonTypeCustom];
[btnYellow setBackgroundColor: [UIColor yellowColor]];
btnYellow = [self buttonTraits:btnYellow];
[btnYellow setFrame: CGRectMake(20, 20, 280, 30)];
[btnYellow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnYellow.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
btnRed = [UIButton buttonWithType: UIButtonTypeCustom];
[btnRed setBackgroundColor: [UIColor redColor]];
[btnRed setFrame: CGRectMake(20, 60, 280, 30)];
[btnRed setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnRed.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
btnBlue = [UIButton buttonWithType: UIButtonTypeCustom];
[btnBlue setFrame: CGRectMake(20, 100, 280, 30)];
[btnBlue setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnBlue.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[btnBlue setBackgroundColor: [UIColor blueColor]];
btnGreen = [UIButton buttonWithType: UIButtonTypeCustom];
[btnGreen setFrame: CGRectMake(20, 140, 280, 30)];
[btnGreen setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnGreen.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[btnGreen setBackgroundColor: [UIColor greenColor]];
//add buttons to the view
[self.view addSubview:btnYellow];
[self.view addSubview:btnRed];
[self.view addSubview:btnBlue];
[self.view addSubview:btnGreen];
//set the counting ints to 0
yellowCount = 0, redCount = 0, blueCount = 0, greenCount = 0;
//run through the animations the first time
[self beginAnimations];
}
-(void)beginAnimations
{
if (!(yellowCount==5 && redCount==5 && blueCount == 5 && greenCount == 5))
{
//if you want 5 buttons, define another one, then the next line would be int rand=random()%6;
int rand=((arc4random()%5)+1); //arc4random gives 0-3; add 1 for 1-4
switch (rand) {
case 1:
{
//make sure this button hasn't gone through the process 5 times already
if (yellowCount<5)
{
//increment the button's count
yellowCount++;
//set up animation with 1.5 second duration (alpha decline from 1->.5)
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnYellow setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnYellow setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
//restart the animation hoping to get another button that hasn't gone 5 times yet
[self beginAnimations];
}
}
break; //you forgot a break here. can cause troubles
case 2:
{
if (redCount<5)
{
redCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnRed setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnRed setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break;
case 3:
{
if (blueCount<5)
{
blueCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnBlue setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnBlue setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break; //you forgot another break here. can cause troubles
case 4:
{
if (greenCount<5)
{
greenCount++;
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^
{
[btnGreen setAlpha:.5];
} completion:^(BOOL finished)
{
if(finished==TRUE)
{
[UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{
[btnGreen setAlpha:1];
} completion:^(BOOL finished) {
if(finished==TRUE)
{
[self beginAnimations];
}
}];
}
}];
}
else
{
[self beginAnimations];
}
}
break;
default:
{
//in case of an awry number, restart the process (be wary; infinite loop potential)
[self beginAnimations];
}
break; //it is of good practice to always have a default method in switch statements
}
}
else
{
//the process is complete
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
UIButton *btnYellow;
UIButton *btnRed;
UIButton *btnBlue;
UIButton *btnGreen;
int yellowCount;
int redCount;
int blueCount;
int greenCount;
}
#end
The problem is that in cases 1 and 2, you set the alpha value to 0.5 and then IMMEDIATELY back to 1. So when the animation starts, it is simply 1 and will remain so. Just delete the 2nd assignment, and the animation will change alpha to 0.5. PS: As already mentioned, the random number generated is from 0 to 4, and not from 1 to 5.

Trying to get 2 animations to work simultaneously

In the next code there are 2 methods that are called one after the other. The first one makes the center button disappear and the second one makes the tabbar disappear. Separately they are working fine.
My problem is that when I try to call them one after the other hideCenterButton doesn't animate. Instead of rolling to the left of the screen the button just disappears.
-(void)hideCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}}
...
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//[UIView setAnimationDelay:1];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
You have probably hit into some kind of limitation of UIView-based animations. Several things you might try are:
use animateWithDuration also for hideTabBar:: indeed, beginAnimation is the old-way of animating UIView; animateWithDuration is more recent (introduced with iOS4); by using the same animation calls in bot cases you might get better results; this should be straightforward; this should work:
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
}
completion:nil];
}
rewrite your animation using Core Animation: this is a slightly lower-level mechanism but it allows you to get the best results in terms of performance; this is, e.g., how you could rewrite the first animation:
first you need to provide a callback:
- (void)animationDidStop:(CABasicAnimation*)anim finished:(BOOL)flag {
CALayer* layer = [anim valueForKey:#"animationLayer"];
layer.position = [anim.toValue CGPointValue];
}
then you can animate your button like this:
CABasicAnimation* move = [CABasicAnimation animationWithKeyPath:#"position"];
CGPoint pos = self.centerButton.center;
pos.x -= 100;
move.toValue = [NSValue valueWithCGPoint:pos];
move.duration = 0.3;
move.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
move.removedOnCompletion = NO;
move.fillMode = kCAFillModeForwards;
move.autoreverses = NO;
move.delegate = self;
[move setValue:self.centerButton.layer forKey:#"animationLayer"];
[self.centerButton.layer addAnimation:move forKey:#"buttonPosition"];
Using Core Animation, you will end up writing more code and you will need to allow some time to learn Core Animation basics, but that was the only way I could solve a similar issue I had with two related animation.
Hope it helps.

Checking if the UIPickerView is shown or hidden? The *if* conditions?

I have a little problem with my UIPickerView animation.
I did set up the animation so when I click a button the UIPickerView will slide up and after another click it will slide down. But I hit the problem on if. I tried to set up a new settings bool and set it's value after each animation, so the if was just checking for the bool. I'm not sure if I did explained it well, but maybe you get the idea from the code.
But unfortunately it's not working... Any ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
settings = [NSUserDefaults standardUserDefaults];
[settings setBool:NO forKey:#"pickerShown"];
[settings synchronize];
}
- (IBAction)showPicker:(id)sender {
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
if ([settings boolForKey:#"pickerShown"] == YES) {
// Perform transform to slide it off the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -0); // Offset.
[UIView commitAnimations];
[settings setBool:NO forKey:#"pickerShown"];
[settings synchronize];
} else if ([settings boolForKey:#"pickerShown"] == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
[settings setBool:YES forKey:#"pickerShown"];
[settings synchronize];
}
}
EDIT:
Just found the solution...
If anyone's interested - here it is:
The h. file:
.
.
.
{
BOOL pickerShown;
}
.
.
.
The .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
pickerShown = NO;
}
- (IBAction)showPicker:(id)sender {
if (pickerShown == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
//[UIView setAnimationDidStopSelector:#selector(hidePicker)];
countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
[UIView commitAnimations];
[self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (pickerShown == YES) {
//countryPicker.hidden = NO;
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
countryPicker.hidden = NO;
}
}
If a view is visible on screen, it's window property will be non-nil.
Just found the solution... If anyone's interested - here it is:
The h. file:
.
.
.
{
BOOL pickerShown;
}
.
.
.
The .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
pickerShown = NO;
}
- (IBAction)showPicker:(id)sender {
if (pickerShown == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
//[UIView setAnimationDidStopSelector:#selector(hidePicker)];
countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
[UIView commitAnimations];
[self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (pickerShown == YES) {
//countryPicker.hidden = NO;
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
countryPicker.hidden = NO;
}
}
You can check it with:
self.countryPicker.superview
If picker is hidden then superview will be nil. Otherwise it will be containing view.
If all you want is the basic effect of displaying the picker view, then removing it again there is an easier way.
pseudocode:
//do any picker initialization here
if(!pickerIsShowing)
[self.view addSubview:yourPickerView];
else
[self.yourPickerView removeFromSuperView];