1

I am tried to after completed animation redirect to Google page I tried like this ;

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    [self close];
    [self.audioPlayer2 play];
    [self open];
}
-(void)close
{
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];
    
    [shake1 setDelegate:self];
    
    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];

}
-(void)open
{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}

But my Problem is Close method not completed but open method is called. But I need after completed Closed method and then call to Open method

Can you tell me what wrong in my code?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Pavan Alapati
  • 317
  • 1
  • 5
  • 17
  • You can use the call back mthod to solve this issue. – Ramesh Muthe Oct 08 '14 at 10:00
  • I guess `[self.audioPlayer2 play];` runs on a different thread so the code keeps running. Like @RameshMuthe said, you need to execute `[self open]` in a call back or a block of completion. – BoilingLime Oct 08 '14 at 10:02

2 Answers2

2

I have changed the your code once check whether your issue resolved

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{

    [self close];
    [self.audioPlayer2 play];

}
-(void)close
{
  [CATransaction begin];
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];

    [shake1 setDelegate:self];

    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
[CATransaction setCompletionBlock:^{ [self open];}];
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];
 [CATransaction commit];
}
-(void)open
{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}

Here I have used CATransaction, it has a completion block handler.

Ramesh Muthe
  • 811
  • 7
  • 15
0

Try putting a completion block:

-(void)close:(void(^)(void))completion
{
    CABasicAnimation *shake1 = [CABasicAnimation animationWithKeyPath:@"position1"];
    [shake1 setDuration:1.6];
    [shake1 setRepeatCount:1];
    [shake1 setAutoreverses:YES];

    [shake1 setDelegate:self];

    firstView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    secondView.frame=CGRectMake(0,230 , self.view.frame.size.width, self.view.frame.size.height/2);
    [firstView.layer addAnimation:shake1 forKey:@"position1"];
    [secondView.layer addAnimation:shake1 forKey:@"position1"];

    completion();
}


-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{

    [self close:^{
        [self.audioPlayer2 play];
        [self open];
    }];

}
aj_f
  • 932
  • 7
  • 12
  • This is not working as what he expected.Let say set the duration to 20 sec then after 20 sec the open method should call, but it is not happening. – Ramesh Muthe Oct 08 '14 at 10:16