我在摇动iPhone后尝试 call Google页面。

- (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时,摇晃的图像未显示但Google页面已打开
因此,请帮助我。我需要在摇动手机时先摇动图像,摇动完成后摇动图像以打开Goog​​le页面。

提前致谢。

最佳答案

将delgate self设置为震动动画,然后在动画完成时委托调用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"]];
    }
希望这就是你想要的。

10-08 07:42