iosobjective-ciphonemethod-callhandshaking

how to call google page after shake


I tried to call google page after shake iPhone. I tried like this:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        [self shakemethod];
        [self open];
      
    }
}
-(void)shakemethod
{
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
        [shake setDuration:1.1];
        [shake setRepeatCount:2];
        [shake setAutoreverses:YES];
        [shake setFromValue:[NSValue valueWithCGPoint:
                             CGPointMake(lockImage.center.x - 15,lockImage.center.y)]];
        [shake setToValue:[NSValue valueWithCGPoint:
                           CGPointMake(lockImage.center.x + 15, lockImage.center.y)]];
        [lockImage.layer addAnimation:shake forKey:@"position"];
}
-(void)open
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}

Two methods working but when i shake iPhone shaking image not showing but google page open. I need when I shake mobile first shake image, after shaking completed shaking Open to Google Page.


Solution

  • set the delgate self to your shake animation then at animation finish delgate call the open method

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if(event.type == UIEventSubtypeMotionShake)
        {
            [self shakemethod];
    
        }
    }
    -(void)shakemethod
    {
    CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
            [shake setDuration:1.1];
            [shake setRepeatCount:2];
            [shake setAutoreverses:YES];
           //set animation delgate to self
            [shake setDelegate:self];
            [shake setFromValue:[NSValue valueWithCGPoint:
                                 CGPointMake(lockImage.center.x - 15,lockImage.center.y)]];
            [shake setToValue:[NSValue valueWithCGPoint:
                               CGPointMake(lockImage.center.x + 15, lockImage.center.y)]];
            [lockImage.layer addAnimation:shake forKey:@"position"];
    }
    
       //when animation will finish call the open method
        -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
        {
                [self open];
        }
        -(void)open
        {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
        }
    

    Hope this is what you want.