touches ended is causing issues with segue animation - objective-c

Im using the below code for touches ended. After a few uiviews are moved around a bit I call a segue in my code like this:
[self performSegueWithIdentifier: #"segueToLevel2" sender: self]
The segue's transition types are set to "cross dissolve".
With the code below everything works fine until I get to level 5. On level five when the segue is called it does a corner to corner flip instead of the cross dissolve and from this point on every segue in my app does this flip instead of what they were set to do. If I take out my touches ended method everything works as expected so the problem must be here. I just cant figure out why this works for levels 1-4 but craps out on level 5. Any help would be greatly appreciated....im loosing my mind here.
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
switch (level) {
case 1:{
if ((piece11.hidden == YES) && (piece1moving.hidden == NO) && (piece1placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece1moving.frame = CGRectMake(-65, 153, 490, 422);
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:3 target:(self) selector:#selector(moveToFront) userInfo:(nil) repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:#selector(singlePieceCallback) userInfo:(nil) repeats:NO];
}
}
break;
case 2:{
UITouch *touch1 = [[event allTouches] anyObject];
if (([touch1 view] == piece1) && (piece1placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece1.frame = CGRectMake(47, 402, 305, 312);
[UIView commitAnimations];
}
if (([touch1 view] == piece2) && (piece2placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece2.frame = CGRectMake(34, 4, 291, 399);
[UIView commitAnimations];
}
}
break;
case 3:{
UITouch *touch1 = [[event allTouches] anyObject];
if (([touch1 view] == piece1) && (piece1placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece1.frame = CGRectMake(1, 419, 308, 356);
[UIView commitAnimations];
}
if (([touch1 view] == piece2) && (piece2placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece2.frame = CGRectMake(-7, 67, 291, 315);
[UIView commitAnimations];
}
if (([touch1 view] == piece3) && (piece3placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece3.frame = CGRectMake(-16, 169, 427, 310);
[UIView commitAnimations];
}
}
break;
case 4:{
UITouch *touch1 = [[event allTouches] anyObject];
if (([touch1 view] == piece1) && (piece1placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece1.frame = CGRectMake(0, 76, 219, 261);
[UIView commitAnimations];
}
if (([touch1 view] == piece2) && (piece2placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece2.frame = CGRectMake(89, 485, 255, 287);
[UIView commitAnimations];
}
if (([touch1 view] == piece3) && (piece3placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece3.frame = CGRectMake(103, 197, 305, 330);
[UIView commitAnimations];
}
if (([touch1 view] == piece4) && (piece4placedstate == 0)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece4.frame = CGRectMake(5, 277, 228, 294);
}
}
break;
case 5:{
UITouch *touch1 = [[event allTouches] anyObject];
if (([touch1 view] == piece1) && (piece1placedstate == 0)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece1.frame = CGRectMake(115, 89, 247, 263);
[UIView commitAnimations];
}
if (([touch1 view] == piece2) && (piece2placedstate == 0)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece2.frame = CGRectMake(14, 497, 211, 271);
[UIView commitAnimations];
}
if (([touch1 view] == piece3) && (piece3placedstate == 0)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece3.frame = CGRectMake(92, 248, 244, 272);
[UIView commitAnimations];
}
if (([touch1 view] == piece4) && (piece4placedstate == 0)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece4.frame = CGRectMake(87, 458, 228, 294);
}
if (([touch1 view] == piece5) && (piece5placedstate == 0)) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece5.frame = CGRectMake(-13, 102, 244, 303);
}
}
break;
default:
break;
}

I'm still not sure what was causing my issue with the above code but I have found a solution in re-writing my code and using a UIPanGestureRecognizer like the one below attached to each of the pieces. This works perfectly throughout.
- (void) dragGesture5:(UIPanGestureRecognizer *) panGesture {
CGPoint translation = [panGesture translationInView:self.view];
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:{
originalCenter = piece5.center;
[self.view bringSubviewToFront:piece5];
}
break;
case UIGestureRecognizerStateChanged:{
piece5.center = CGPointMake(piece5.center.x + translation.x,
piece5.center.y + translation.y);
[self checkColision];
}
break;
case UIGestureRecognizerStateEnded:{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
piece5.center = originalCenter;
[UIView commitAnimations];
}
break;
default:
break;
}
[panGesture setTranslation:CGPointZero inView:self.view];
}

Related

cocoa touch trying to delay the animation of the uiview before it pops out again

I have a UIView that serves as the container for 2 tableviews. I have two buttons that controls how data is loaded on those tableviews. Basically when 1 button is tapped the uiview slides out to show the tableview related to that button, and when the other button gets tapped I need it to:
close
hide the 1st tableview
then unhides the 2nd tableview
then uiview slides back out
Here's what I have
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.fighterTableView setHidden:YES];
[self.matchTableView setHidden:NO];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[UIView commitAnimations];
[self.fighterTableView setHidden:YES];
[self.matchTableView setHidden:NO];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
The problem here is on the commitanimations in the else statement I'm trying to set the hidden properties then pop the uiview out again. What's happening is it just hides and unhides the tableview but the animation never happens. I feel like I need to use a delay, but Idk how, unless there's a more decent way of handling this??
Thoughts?
Instead of making use of setHidden method. Why don't you try using the setAlpha method.
It will be something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.fighterTableView setAlpha:0.0];
[self.matchTableView setAlpha:1.0];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[UIView commitAnimations];
[self.fighterTableView setAlpha:0.0];
[self.matchTableView setAlpha:1.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
I would suggest you perform
[UIView setAnimationDidStopSelector:#selector(myAnimationMethod)]
Instead of setting the alpha to 1.0 of the matchTableView set it inside the myAnimationMethod.
So something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(myAnimationMethodDidFinish:)]
if(!isTableOpen){
[self.fighterTableView setAlpha:0.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[self.fighterTableView setAlpha:0.0];
[UIView commitAnimations];
}
-(void) myAnimationMethodDidFinish:(id) sender {
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.matchTableView setAlpha:1.0];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
[self.matchTableView setAlpha:1.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
}

fade in, fade out animation to uilabel

i have a label that i want to fade in and then to fade out.
here is my code:
-(void) fadein
{
scoreLabel.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 1;
[UIView commitAnimations];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 0;
[UIView commitAnimations];
}
from this code i get this situation: my label is fade in and then i don't see the fadeout animation.
how can i fix it?
-(void) fadein
{
scoreLabel.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//don't forget to add delegate.....
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 1;
//also call this before commit animations......
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 0;
[UIView commitAnimations];
}
The call to setAnimationDidStopSelector should be before commit the animations:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
scoreLabel.alpha = 1;
[UIView commitAnimations];

Slide UITableView Off Screen?

I am trying to animate my UITableView sliding off the screen, I have tried this below without success:
-(void)slideTableViewOffScreen
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
stageSelectionTable.frame = CGRectMake(stageSelectionTable.frame.origin.x - stageSelectionTable.frame.size.width, stageSelectionTable.frame.origin.y, stageSelectionTable.frame.size.height, stageSelectionTable.frame.size.width);
[UIView commitAnimations];
}
Any ideas why it might not work, or what I need to do for this animation to work? When that code is called, nothing happens.
Turns out this code works without any weirdness:
-(void)slideTableViewOffScreen
{
CGRect newFrame = stageSelectionTable.frame;
newFrame.origin.x -= 130;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
stageSelectionTable.frame = newFrame;
[UIView commitAnimations];
}

how to animate a UIView same as the working of presentModalViewController ( ie [self presentModalViewController:child animated:YES];)

I am new in ios development, Now i am doing some animation on my application.In my application has one menu on the bottom of main view and have two button one for hide the menu and other for show the menu .My needs is the show and hide function of menu is working like
the [self presentModalViewController:menuView animated:YES]; and [self dismissModalViewControllerAnimated:YES]; function(ie. click the show button ,pop the menuView from the bottom of main view and click the hide button ,move down the menuView ) . I know the basic animation like:
[UIView beginAnimations:#"ShowHideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationOptionOverrideInheritedCurve];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[menuView setAlpha:0];
[UIView commitAnimations];
If any body know ,please help me.
When you tap showMenuView, do as following,
- (IBAction)showView:(id)sender
{
[self.view addSubview: menuView];
CGRect rect = menuView.frame;
rect.origin.y = 480;
menuView.frame = rect;
[UIView beginAnimations:#"ShowView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
rect.origin.y = 0;
menuView.frame = rect;
[UIView commitAnimations];
}
And to hide,
- (IBAction)hideView:(id)sender
{
CGRect rect = menuView.frame;
[UIView beginAnimations:#"HideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
rect.origin.y = 480;
menuView.frame = rect;
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[menuView removeFromSuperview];
}

Core Animation question about swapping views

-(IBAction)buttonPressed1:(id)sender
{
SignView *tempVC = [[SignView alloc] initWithNibName:#"SignView" bundle:Nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:0.2f];
[self presentModalViewController:tempVC animated:YES];
[tempVC passDataWithString:button1.titleLabel.text andColor:currentlySelectedColor isNightModeOn:nightMode.on];
[UIView commitAnimations];
}
can anyone help me figure out why this code doesn't work?
This method will call beginAnimations: and commitAnimations, which cannot be nested.
[self presentModalViewController:tempVC animated:YES];
So move it to before beginAnimations: or after commitAnimations.