2

摇晃iPhone后我尝试调用谷歌页面。我尝试过这样

- (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 时摇动图像不显示但谷歌页面打开所以请帮助我。我需要当我摇动手机时第一次摇动图像,摇动完成后摇动打开到谷歌页面。

先谢谢了。

4

1 回答 1

0

将 delgate self 设置为您的摇动动画,然后在动画完成时 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"]];
    }

希望这是你想要的。

于 2014-10-07T12:09:54.417 回答