2014-10-07 62 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