2014-10-07 76 views
2

后,谷歌网页我试着打电话给谷歌网页后摇iPhone.i试过这样如何调用抖动

- (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"]]; 
} 

两种方法的工作,但是当我摇晃iPhone晃动的图像不显示谷歌网页打开 所以请但是帮助我。我需要当我摇动手机第一次摇动图像,摇晃完毕后摇晃打开Goog​​le页面。

感谢高级。

回答

1

设置delgate自我的抖动动画,然后在动画完成delgate调用open方法

- (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"]]; 
    } 

希望这是你想要的。

+0

感谢您的重播 – 2014-10-07 12:38:37