How to copy cells between tables as the pulse app - cocoa-touch

I need to copy cells between 2 table views.
I have a more-or-less working solution. However, it is not smooth, and I would love to do something similar to the pulse app reorder feeds option...
Something special in the way that pulse works, is that the reorder animation is the same as in a normal reorder of cells, but still the cell can move between tables.
This is what I have now:
- (BoardColumnView *) addColumn:(NSString *)title type:(ColumnType)type{
BoardColumnView *col = [BoardColumnView createColumn:title];
[self.columns addObject:col];
// Add a pinch gesture recognizer to the table view.
UILongPressGestureRecognizer* draggingRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(moveActionGestureRecognizerStateChanged:)
];
draggingRecognizer.minimumPressDuration = 0.5;
draggingRecognizer.delegate = self;
[col.tableView addGestureRecognizer:draggingRecognizer];
[draggingRecognizer release];
return col;
}
#pragma mark -
#pragma mark UIGestureRecognizer Delegate/Actions
- (BOOL) gestureRecognizerShouldBegin: (UIGestureRecognizer *) gestureRecognizer
{
ALog(#"Drag detected");
return YES;
}
- (void) moveActionGestureRecognizerStateChanged: (UIGestureRecognizer *) recognizer
{
switch ( recognizer.state )
{
default:
case UIGestureRecognizerStateFailed:
// do nothing
break;
case UIGestureRecognizerStatePossible:
case UIGestureRecognizerStateCancelled:
{
ALog(#"Canceled");
[UIView beginAnimations: #"SnapBack" context: NULL];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(finishedSnap:finished:context:)];
CGRect f = self.dragView.frame;
f.origin = _dragOriginCellOrigin;
self.dragView.frame = f;
[UIView commitAnimations];
break;
}
case UIGestureRecognizerStateEnded:
{
// move the real cell into place
[UIView beginAnimations: #"SnapToPlace" context: NULL];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(finishedSnap:finished:context:)];
CGRect r = self.dragView.frame;//[self.content rectForItemAtIndex: _emptyCellIndex];
CGRect f = self.dragView.frame;
f.origin.x = r.origin.x + floorf((r.size.width - f.size.width) * 0.5);
f.origin.y = r.origin.y + floorf((r.size.height - f.size.height) * 0.5);
NSLog( #"Gesture ended-- moving to %#", NSStringFromCGRect(f) );
self.dragView.frame = f;
self.dragView.transform = CGAffineTransformIdentity;
self.dragView.alpha = 1.0;
[UIView commitAnimations];
break;
}
case UIGestureRecognizerStateBegan:
{
ALog(#"Start move..");
// find the cell at the current point and copy it into our main view, applying some transforms
_lastColumn = (BoardColumnView *)[(UITableView *)recognizer.view superview];
_targetColumn = _lastColumn;
if (_lastIndexPath) {
[_lastIndexPath release];
_lastIndexPath = nil;
}
_lastIndexPath = [_lastColumn.tableView indexPathForRowAtPoint:[recognizer locationInView: _lastColumn]];
UITableViewCell *sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];
CGRect frame = [_lastColumn convertRect: sourceCell.frame fromView: self.content];
self.dragView = [[[UIImageView alloc] initWithImage:[sourceCell screenshot]] autorelease];
self.dragView.opaque = YES;
self.dragView.backgroundColor = _lastColumn.backgroundColor;
self.dragView.center = [recognizer locationInView: self.view];
//self.dragView.cornerRadius = 8; // if you like rounded corners
self.dragView.layer.shadowOffset = CGSizeMake(-5, 2);
self.dragView.layer.shadowRadius = 5;
self.dragView.layer.shadowOpacity = 0.5;
[content addSubview: self.dragView];
[content bringSubviewToFront:self.dragView];
// grab some info about the origin of this cell
_dragOriginCellOrigin = frame.origin;
[UIView beginAnimations: #"" context: NULL];
[UIView setAnimationDuration: 0.2];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
// transformation-- larger, slightly transparent
self.dragView.transform = CGAffineTransformMakeScale( 1.2, 1.2 );
self.dragView.alpha = 0.7;
[UIView commitAnimations];
/* // reload the grid underneath to get the empty cell in place
[self.content reloadItemsAtIndices: [NSIndexSet indexSetWithIndex: index]
withAnimation: AQGridViewItemAnimationNone];
*/
break;
}
case UIGestureRecognizerStateChanged:
{
// update draging cell location
self.dragView.center = [recognizer locationInView: self.view];
//Determinar sobre que columna esta flotando..
CGRect global;
if (_targetColumn) {
_targetColumn = nil;
}
for (BoardColumnView *col in self.columns) {
UITableView *table = col.tableView;
global = [table convertRect:self.dragView.frame fromView:content];
if ([table pointInside:global.origin withEvent:nil]) {
ALog([NSString stringWithFormat: #"Esta sobre la tabla %#", col.column.name]);
_targetColumn = col;
break;
}
}
if ( !_targetColumn )
{
ALog(#"Esta en zona muerta");
}
break;
}
}
}
- (void) finishedSnap: (NSString *) animationID finished: (NSNumber *) finished context: (void *) context
{
if (_targetColumn && _lastIndexPath) {
ALog(#"Moviendo a nuevo destino");
CGRect frame = [_targetColumn convertRect: self.dragView.frame fromView: self.content];
NSIndexPath *destPath = [_targetColumn.tableView indexPathForRowAtPoint: frame.origin];
[UIView beginAnimations: #"" context: NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
self.dragView.backgroundColor = _targetColumn.backgroundColor;
//Si inserta entre tareas en destino...
_lastColumn.totalRows -= 1;
_targetColumn.totalRows += 1;
[_lastColumn.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:_lastIndexPath] withRowAnimation:UITableViewRowAnimationTop];
if (destPath) {
[_targetColumn.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationBottom];
} else {
[_targetColumn.tableView reloadData];
}
[UIView commitAnimations];
} else {
ALog(#"Cancelado el moviento, regresar a home");
UITableViewCell * sourceCell = [_lastColumn.tableView cellForRowAtIndexPath:_lastIndexPath];
if (sourceCell) {
[UIView beginAnimations: #"Back" context: NULL];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
//self.dragView.transform = CGAffineTransformIdentity;
self.dragView.frame = [self.content convertRect: sourceCell.frame fromView: _lastColumn];
//self.dragView.alpha = 0.0;
[UIView commitAnimations];
}
}
}
- (void)animationDidStop:(id)animationID finished:(BOOL)flag context:(id)context {
// dismiss our copy of the cell
ALog(#"Eliminando drag view");
[self.dragView removeFromSuperview];
self.dragView = nil;
_lastColumn = nil;
_targetColumn = nil;
if (_lastIndexPath) {
_lastIndexPath = nil;
}
}
UPDATE: I've created an open source project to show my progress so far:
https://bitbucket.org/elmalabarista/dragdroptableviews

Related

iOS: How to drag a view along with finger till a particular y offset?

I am dragging a view from bottom to top with pre-defined offset positions using pan gesture.
My current implementation is such that the view updates after the gesture has ended. However, I want the view to update along with the finger (gesture is ongoing) and stop at the predefined (next) position when the gesture has ended.
Would appreciate if somebody could point me in the right direction. Thank you.
m_openPercentage = #"30,60";
- (void)onContentViewPanned:(UIPanGestureRecognizer *)gesture
{
CGPoint velocity = [gesture velocityInView:self.view];
NSArray *m_Openpercentage = [m_OpenPercentage componentsSeparatedByString:#","];
NSMutableArray *fullpercentage = [NSMutableArray arrayWithArray:m_Openpercentage];
float diffy = 0.0;
if(([gesture state] == UIGestureRecognizerStateEnded))
{
if(velocity.y < 0)
{
if(m_isfullscreen && ![fullpercentage containsObject:#"100"])
{
[fullpercentage addObject:#"100"];
}
diffy = 1-([[fullpercentage objectAtIndex:counter] floatValue]/100);
CGFloat ypos = self.view.frame.size.height * diffy;
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
[self.view setNeedsLayout];
self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
if(self.view.frame.origin.y==0)
[self showNavigationbar:1];
} completion:^(BOOL finished) {
}];
if(counter<[fullpercentage count]-1)
counter++;
}
else
{
if(counter>0)
{
float diffy = 1-([[fullpercentage objectAtIndex:counter-1] floatValue]/100);
CGFloat ypos = self.view.frame.size.height * diffy;
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
} completion:^(BOOL finished) {
}];
}
else
{
diffy = 1-(m_initialPercentage/100);
CGFloat ypos = self.view.frame.size.height * diffy;
if(self.view.frame.origin.y < ypos)
{
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
self.view.frame = CGRectMake(0,ypos,self.view.frame.size.width,self.view.frame.size.height);
} completion:^(BOOL finished) {
}];
}
else
{
[self removeoverlayview];
[self.view removeFromSuperview];
id tmpControllerObj = [parentController getParentController];
[tmpControllerObj view].userInteractionEnabled = YES;
}
}
if((counter-1)>=0)
counter--;
[self showNavigationbar:0];
}
if(![m_Openpercentage containsObject:#"100"] && m_isfullscreen)
[self updatefullview];
}
}

UIScrollView does not move view ontop of keyboard iPad

I have several text fields on screen. When I tap inside of the one towards the bottom, the keyboard appears over them.
I've referred to apple's documentation and used the following code to try and tackle this.
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
What i'd like to happen, when the keyboard appears the text fields that in side the scroll field will move up. Any help is appreciated.
Use This Code for Move the view up..........
-(void) textFieldDidBeginEditing:(UITextField*)textField
{
[self slideViewUpForTextField:textField];
}
-(void) slideViewUpForTextField:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 3.0 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
heightFraction = 0.0;
else if (heightFraction > 1.0)
heightFraction = 1.0;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
else
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
And write these on top of the class
And After #import"".....
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 158;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
This is also write ....
-(void) textFieldDidEndEditing:(UITextField*) textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}

uiview animatewithduration completion continues after view is dismissed

I have a modelViewController that contains a UIView animation. When the animation block finishes it calls itself, thus looping.
When I dismiss the modelViewController (dismissInfo) which calls [_starView removeFromSuperview], the function gets called over and over very rapidly with the NSLog line being printed multiple times a second.
#implementation InfoVC
{
NSArray *imgs;
NSString *currentImg;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_imageview.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
imgs = [NSArray arrayWithObjects:
#"01.jpg",
#"02.jpg",
#"03.jpg",
#"04.jpg",
#"05.jpg",
#"06.jpg",
nil];
_imgInt = (arc4random()%6);
[self initialImage];
}
- (void)viewWillAppear:(BOOL)animated{
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(#"viewDidAppear");
}
- (void)initialImage
{
_starView.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
_starView = [[UIImageView alloc] initWithImage:image];
// Size the image view to the image (it's bigger)
_starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);
NSLog(#"tarView.center %#", NSStringFromCGPoint(_starView.center) );
_starView.alpha=1;
int nextImgInt = _imgInt + 1 ;
if (nextImgInt>5)
{
nextImgInt=0;
}
NSString *nextImg = [imgs objectAtIndex:nextImgInt];
UIImage *nextImage = [UIImage imageNamed:nextImg];
[_imageviewUnder setImage:nextImage];
[self.view sendSubviewToBack:_imageviewUnder];
_imgInt++;
if (_imgInt>5) {
_imgInt=0;
}
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:7.6f
delay:0.1f
options:UIViewAnimationCurveLinear
animations:^{
[_starView setCenter:CGPointMake(112, 208)];
[_starView setAlpha:0.0f];
}
completion:^(BOOL finished){
[_starView removeFromSuperview];
[self initialImage];
}];
[self.view insertSubview:_starView atIndex:1];
}
- (void)visitTwitter
{
NSURL *URL = [NSURL URLWithString:#"http://twitter.com/"];
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithURL:URL];
[self.navigationController pushViewController:webViewController animated:YES];
}
- (IBAction)dismissInfo:(id)sender
{
[self cleanup];
[self dismissModalViewControllerAnimated:YES];
}
- (void)cleanup
{
[_starView.layer removeAllAnimations];
[self.view.layer removeAllAnimations];
[_starView removeFromSuperview];
}
- (void)viewDidUnload
{
[self cleanup];
[super viewDidUnload];
}
#end
Make yourself an animating flag, set true in viewDidLoad, and false in cleanup. Then check it in your initialImage method:
if ( ! animating )
return;
This is the second time I've come across this. One possible cause is the method being initialised in viewDidLoad but the subview and image not being ready in time (it seems the first pass of the animation loop doesn't work adding a ghost to the machine).
I moved the call to ViewDidAppear, making sure to display an initialisation image to stop the white flash. At last an infinite animation in a modal window
- (void)viewDidLoad
{
[super viewDidLoad];
_imageview.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
imgs = [NSArray arrayWithObjects:
#"01.jpg",
#"02.jpg",
#"03.jpg",
#"04.jpg",
#"05.jpg",
#"06.jpg",
nil];
_imgInt = (arc4random()%6);
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
[_imageviewUnder setImage:image];
}
- (void)viewDidAppear:(BOOL)animated{
[self initialImage];
}
- (void)initialImage
{
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
_starView = [[UIImageView alloc] initWithImage:image];
// Size the image view to the image (it's bigger)
_starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);
NSLog(#"initialImage");
_starView.alpha=1;
[self.view insertSubview:_starView atIndex:1];
int nextImgInt = _imgInt + 1 ;
if (nextImgInt>5)
{
nextImgInt=0;
}
NSString *nextImg = [imgs objectAtIndex:nextImgInt];
UIImage *nextImage = [UIImage imageNamed:nextImg];
[_imageviewUnder setImage:nextImage];
[self.view sendSubviewToBack:_imageviewUnder];
_imgInt++;
if (_imgInt>5) {
_imgInt=0;
}
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:7.6];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//[UIView setAnimationDidStopSelector:#selector(initialImage) ];
[_starView setCenter:CGPointMake(112, 208)];
[_starView setAlpha:0.0f];
[UIView commitAnimations];
[self performSelector:#selector(initialImage) withObject:nil afterDelay:7.6];
}
- (IBAction)dismissInfo:(id)sender
{
[self cleanup];
[self dismissModalViewControllerAnimated:YES];
}
- (void)cleanup
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[_starView.layer removeAllAnimations];
[self.view.layer removeAllAnimations];
[_starView removeFromSuperview];
}
If you just want to create a looping animation, the best thing to do is use the option: 'UIViewAnimationOptionRepeat'. For example:
[UIView animateWithDuration:2.0
delay:0.0f
options:UIViewAnimationOptionRepeat
animations:^{
[myUIViewThing setCenter:CGPointMake(myUIViewThing.center.x - 100, myUIViewThing.center.y)];
}
completion:nil];

Flip animation in sub-UIView donĀ“t found

I want flip movement in my subview.
In next method change my subview but without flip animation....
int tag=g.view.tag;
UIViewController* vc2 = [controlersOfTableBoxes objectAtIndex:tag-1];
UIView* viewOfSelf = self.view;
UIViewController* v = [[UIViewController alloc] init];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:v.view cache:NO];
int maxGraphs = [[viewOfSelf subviews] count];
for (int i = 0; i < maxGraphs; i++ )
{
if([[[viewOfSelf subviews] objectAtIndex:i] isKindOfClass:[UIView class]])
{
UIView* v = [[viewOfSelf subviews] objectAtIndex:i];
if(v.tag==tag)
{
[[[viewOfSelf subviews] objectAtIndex:i] removeFromSuperview];
break;
}
}
}
[viewOfSelf addSubview:vc2.view];
v.view = viewOfSelf;
[UIView commitAnimations];
If i change this lines:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:v.view cache:NO];
for
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
the movement is in all of screen but i want only in subview in subviewcontroller.
I try to this method:
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationTransitionFlipFromRight animations:^(void)
{
int tag=g.view.tag;
UIViewController* vc2 = [controlersOfTableBoxes objectAtIndex:tag-1];
UIView* viewOfSelf = self.view;
UIViewController* v = [[UIViewController alloc] init];
int maxGraphs = [[viewOfSelf subviews] count];
for (int i = 0; i < maxGraphs; i++ )
{
if([[[viewOfSelf subviews] objectAtIndex:i] isKindOfClass:[UIView class]])
{
UIView* view = [[viewOfSelf subviews] objectAtIndex:i];
if(view.tag==tag)
{
v.view=view;
[[[viewOfSelf subviews] objectAtIndex:i] removeFromSuperview];
break;
}
}
}
[viewOfSelf addSubview:vc2.view];
v.view = viewOfSelf;
}
completion:^(BOOL finished)
{}];
but the result is the same...
you can use
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];
[UIView transitionFromView:view2 toView:view1 duration:3 options:UIViewAnimationOptionTransitionFlipFromBottom completion:NULL];
[UIView commitAnimations];
try
i
nt tag=g.view.tag;
UIViewController* v = [controlersOfBoxes objectAtIndex:tag-1];
UIViewController* vc2 = [controlersOfTableBoxes objectAtIndex:tag-1];
[UIView transitionWithView:vc2.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^ { [v.view removeFromSuperview]; [self.view addSubview:vc2.view]; } completion:nil];

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